I was wondering, which is faster (in Pike) or are they eventually the same? if(search(someArray, someElt) < 0){;} or if(search(someArray, someElt) == -1){;}
If one is faster, why?
Greetings,
Coen
__________________________________________________________ Deze e-mail en de inhoud is vertrouwelijk en uitsluitend bestemd voor de geadresseerde(n). Indien u niet de geadresseerde bent van deze e-mail verzoeken wij u dit direct door te geven aan de verzender door middel van een reply e-mail en de ontvangen e-mail uit uw systemen te verwijderen. Als u geen geadresseerde bent, is het niet toegestaan om kennis te nemen van de inhoud, deze te kopieren, te verspreiden, bekend te maken aan derden noch anderszins te gebruiken.
The information contained in this e-mail is confidential and may be legally privileged. It is intended solely for the addressee. If you are not the intended recipient, any disclosure, copying, distribution or any action taken or omitted to be taken in reliance on it, is prohibited and may be unlawful. Please notify us immediately if you have received it in error by reply e-mail and then delete this message from your system. __________________________________________________________
I think they are pretty much the same, but for clarity I usually use the has_index() function instead.
I made a small testprogram for you, so that you can benchmark it youself:
int main(int argc, array(string) argv) { int len = (int)argv[1];
array x = allocate(len, random); int rpt = (int)argv[2];
int elem = -1;
werror("Running %d iterations on array of size %d\n", rpt, len);
float t = gauge { for(int i=0; i < rpt; i++) { if (search(x, elem) == -1) { ; }; } };
werror("==-1 took %fs\n", t);
t = gauge { for(int i=0; i < rpt; i++) { if (search(x, elem) < 0) { ; }; } };
werror("<0 took %fs\n", t);
t = gauge { for(int i=0; i < rpt; i++) { if (has_index(x, elem)) { ; }; } };
werror("has_index() took %fs\n", t);
return 0; }
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..
Thanks for the sample program, why didn't I think of that myself :)
Marcus Agehall (Roxen IS) @ Pike (-) developers forum wrote:
I think they are pretty much the same, but for clarity I usually use the has_index() function instead.
I made a small testprogram for you, so that you can benchmark it youself:
int main(int argc, array(string) argv) { int len = (int)argv[1];
array x = allocate(len, random); int rpt = (int)argv[2];
int elem = -1;
werror("Running %d iterations on array of size %d\n", rpt, len);
float t = gauge { for(int i=0; i < rpt; i++) { if (search(x, elem) == -1) { ; }; } };
werror("==-1 took %fs\n", t);
t = gauge { for(int i=0; i < rpt; i++) { if (search(x, elem) < 0) { ; }; } };
werror("<0 took %fs\n", t);
t = gauge { for(int i=0; i < rpt; i++) { if (has_index(x, elem)) { ; }; } };
werror("has_index() took %fs\n", t);
return 0; }
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..
__________________________________________________________ Deze e-mail en de inhoud is vertrouwelijk en uitsluitend bestemd voor de geadresseerde(n). Indien u niet de geadresseerde bent van deze e-mail verzoeken wij u dit direct door te geven aan de verzender door middel van een reply e-mail en de ontvangen e-mail uit uw systemen te verwijderen. Als u geen geadresseerde bent, is het niet toegestaan om kennis te nemen van de inhoud, deze te kopieren, te verspreiden, bekend te maken aan derden noch anderszins te gebruiken.
The information contained in this e-mail is confidential and may be legally privileged. It is intended solely for the addressee. If you are not the intended recipient, any disclosure, copying, distribution or any action taken or omitted to be taken in reliance on it, is prohibited and may be unlawful. Please notify us immediately if you have received it in error by reply e-mail and then delete this message from your system. __________________________________________________________
[...]
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...
I pretty much figured that the optimizer would do something like that. :) I do think that side-effect free loops could be removed completly.
Now the loops are optimized away as well:
| ./pike -mmaster.pike lyslyskom16362790.pike 471111 1000000000 | Running 1000000000 iterations on array of size 471111 | ==-1 took 0.000s | <0 took 0.000s | has_index() took 0.000s
I have no clue what your test code does, but your intentions sounds good! :)
pike-devel@lists.lysator.liu.se