[...]
Sample output on my machine: chromaggus:tools agehall$ pike ~/fun/search_speed.pike 471111 1000000000 Running 1000000000 iterations on array of size 471111 ==-1 took 16.390s <0 took 16.490s has_index() took 16.290s
As you can see, there are no major difference between the three..
That's mostly due to the optimizer having removed the differences:
Coding: {&$2=(int){index($1,const(1))}}; {&$3=const(allocate)($2,const(random)), &$4=(int){index($1,const(2))}, &$5=const(-1)}; {const(Fd(2)->write)(const("Running %d iterations on array of size %d\n"),$4,$2)}; {&$6=const(`/)(const(`-)(const(`-)(const(gethrvtime)(const(1)),{{&$6=const(-1)}; ++Loop($4,&$6)}, const(gethrvtime)(const(1)))),const(1000000000.0))}; {const(Fd(2)->write)(const("==-1 took %fs\n"),$6)}; {&$6=const(`/)(const(`-)(const(`-)(const(gethrvtime)(const(1)),{{&$7=const(-1)}; ++Loop($4,&$7)}, const(gethrvtime)(const(1)))),const(1000000000.0))}; {const(Fd(2)->write)(const("<0 took %fs\n"),$6)}; {&$6=const(`/)(const(`-)(const(`-)(const(gethrvtime)(const(1)),{{&$7=const(-1)}; ++Loop($4,&$7)}, const(gethrvtime)(const(1)))),const(1000000000.0))}; {const(Fd(2)->write)(const("has_index() took %fs\n"),$6)}; return(const(0)); return(const(0))
As you can see above, the actual calls to search() and has_index() have been removed (the code was side-effect free), so what you've benchmarked is just three identical empty loops.
Yes, Pike's optimizer is aggressive.
Hmm... the question is if we shouldn't optimize away those as well...