Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

This is my attempt to answer my own equally named question on SO my own equally named question on SO. In this case, I need a method comparing two strings so that the running time is input independent.

This is my attempt to answer my own equally named question on SO. In this case, I need a method comparing two strings so that the running time is input independent.

This is my attempt to answer my own equally named question on SO. In this case, I need a method comparing two strings so that the running time is input independent.

deleted 2 characters in body
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

It's a really short snippet and I'm mainly interested in ensuring that it really works in constant time. In particular, I'm afraid that the loop could indeed get optimized to something like

int i = 0
for (; ; ++i) {
 if (input.charAt(i) != secret.charAt(i)) break;
}
for (; i < length; ++i) {
 delta += input.charAt(i) ^ secret.charAt(i);
}

where the first loop could be faster on some architectures, as -- after unrolling -- it translates to load, xor with memory, and a conditional jump. Assuming some future architecture capable of executing 6 instructions per cycle when there are no data dependencies and no mispredictions, it could be a win. Or the JIT could believe, it makes sense...

It's a really short snippet and I'm mainly interested in ensuring that it really works in constant time.

It's a really short snippet and I'm mainly interested in ensuring that it really works in constant time. In particular, I'm afraid that the loop could indeed get optimized to something like

int i = 0
for (; ; ++i) {
 if (input.charAt(i) != secret.charAt(i)) break;
}
for (; i < length; ++i) {
 delta += input.charAt(i) ^ secret.charAt(i);
}

where the first loop could be faster on some architectures, as -- after unrolling -- it translates to load, xor with memory, and a conditional jump. Assuming some future architecture capable of executing 6 instructions per cycle when there are no data dependencies and no mispredictions, it could be a win. Or the JIT could believe, it makes sense...

Rollback to Revision 3
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

##Update due to Ext3h's comment

I'm afraid, the loop could indeed get optimized to something like

int i = 0
for (; ; ++i) {
 if (input.charAt(i) != secret.charAt(i)) break;
}
for (; i < length; ++i) {
 delta += input.charAt(i) ^ secret.charAt(i);
}

where the first loop could be faster on some architectures, as -- after unrolling -- it translates to load, xor with memory, and a conditional jump. Assuming some future architecture capable of executing 6 instructions per cycle when there are no data dependencies and no mispredictions, it could be a win. Or the JIT could believe, it makes sense...

##Update due to Ext3h's comment

I'm afraid, the loop could indeed get optimized to something like

int i = 0
for (; ; ++i) {
 if (input.charAt(i) != secret.charAt(i)) break;
}
for (; i < length; ++i) {
 delta += input.charAt(i) ^ secret.charAt(i);
}

where the first loop could be faster on some architectures, as -- after unrolling -- it translates to load, xor with memory, and a conditional jump. Assuming some future architecture capable of executing 6 instructions per cycle when there are no data dependencies and no mispredictions, it could be a win. Or the JIT could believe, it makes sense...

added 583 characters in body
Source Link
maaartinus
  • 13.6k
  • 1
  • 35
  • 74
Loading
Notice removed Draw attention by maaartinus
Bounty Ended with Emily L.'s answer chosen by maaartinus
Tweeted twitter.com/StackCodeReview/status/743376842644721664
added 243 characters in body
Source Link
maaartinus
  • 13.6k
  • 1
  • 35
  • 74
Loading
Notice added Draw attention by maaartinus
Bounty Started worth 50 reputation by maaartinus
edited tags; edited title
Link
200_success
  • 145.5k
  • 22
  • 190
  • 478
Loading
Source Link
maaartinus
  • 13.6k
  • 1
  • 35
  • 74
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /