Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The code you posted is about half as fast as <code>strcasestr</code>. </p> <pre class="lang-none prettyprint-override"><code>$ gcc -Wall -o my_stristr my_stristr.c steve@solaris:~/code/tmp $ gcc -Wall -o strcasestr strcasestr.c steve@solaris:~/code/tmp $ ./bench ./my_stristr &gt; my_stristr.result ; ./bench ./strcasestr &gt; strcasestr.result; steve@solaris:~/code/tmp $ cat my_stristr.result run 1... time = 6.32 run 2... time = 6.31 run 3... time = 6.31 run 4... time = 6.31 run 5... time = 6.32 run 6... time = 6.31 run 7... time = 6.31 run 8... time = 6.31 run 9... time = 6.31 run 10... time = 6.31 average user time over 10 runs = 6.3120 steve@solaris:~/code/tmp $ cat strcasestr.result run 1... time = 3.82 run 2... time = 3.82 run 3... time = 3.82 run 4... time = 3.82 run 5... time = 3.82 run 6... time = 3.82 run 7... time = 3.82 run 8... time = 3.82 run 9... time = 3.82 run 10... time = 3.82 average user time over 10 runs = 3.8200 steve@solaris:~/code/tmp </code></pre> <p>The <code>main</code> function was: </p> <pre class="lang-c prettyprint-override"><code>int main(void) { char * needle="hello"; char haystack[1024]; int i; for(i=0;i&lt;sizeof(haystack)-strlen(needle)-1;++i) { haystack[i]='A'+i%57; } memcpy(haystack+i,needle, strlen(needle)+1); /*printf("%s\n%d\n", haystack, haystack[strlen(haystack)]);*/ init_stristr(); for (i=0;i&lt;1000000;++i) { /*my_stristr(haystack, needle);*/ strcasestr(haystack,needle); } return 0; } </code></pre> <p>It was suitably modified to test both implementations. I notice as I am typing this up I left in the <code>init_stristr</code> call, but it shouldn't change things too much. <code>bench</code> is just a simple shell script:</p> <pre class="lang-c prettyprint-override"><code>#!/bin/bash function bc_calc() { echo $(echo "scale=4;$1" | bc) } time="/usr/bin/time -p" prog="$1" accum=0 runs=10 for a in $(jot $runs 1 $runs) do echo -n "run $a... " t=$($time $prog 2&gt;&amp;1| grep user | awk '{print $2}') echo "time = $t" accum=$(bc_calc "$accum+$t") done echo -n "average user time over $runs runs = " echo $(bc_calc "$accum/$runs") </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload