Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^4: Odometer pattern iterator (in C).

by BrowserUk (Patriarch)
on May 29, 2015 at 16:43 UTC ( [id://1128318]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Odometer pattern iterator (in C).
in thread Odometer pattern iterator (in C). (Updated.)

That works. But it's about 40% slower than the A::C version I nicked. 11 seconds instead of 8 for the 601 million 16 from 32:

[17:27:38.79] C:\test\humanGenome>junk 32 16 ^C [17:41:10.28] C:\test\humanGenome>junk 32 16 Iters:601080390 [17:41:17.49] C:\test\humanGenome>junk2 32 16 ^C [17:41:40.82] C:\test\humanGenome>junk2 32 16 Iters:601080390 [17:41:52.30] C:\test\humanGenome>

junk2 is your code adapted to count rather than print.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Replies are listed 'Best First'.
Re^5: Odometer pattern iterator (in C).
by Anonymous Monk on May 29, 2015 at 23:02 UTC

    I'm still brushing the rust off my C. :) This one is 50% faster with only slight tweaks. This tight loop is *highly* sensitive to almost any change. BTW, going from -o2 to -o4 doubles speed.

    // inc_c - http://perlmonks.org/?node_id=1128230 // 8.52 secs. for 16/32 count=601080390 -o2 // 3.16 secs. for 16/32 count=601080390 -o4 #include <stdlib.h> #include <stdio.h> #define N 16 // number of elements wanted #define M 32 // place static int place[N+1]; static int count = 0; int step(void) { int *p = place; for(int i = 0; i < N; i++, p++ ) { if(*p < p[1] - 1) { ++*p; while( --i >= 0 ) *--p -= place[0]; return 1; } } return 0; } int main(int argc, char **argv) { int i; int more = 1; for(i = 0; i < N; i++) place[i] = i; place[N] = M; while( more ) { //for(i = 0; i < N; i++) printf(" %d", place[i]); //putchar('\n'); count++; more = step(); } printf("%d\n", count); exit(0); }

      Yes. That now beats the A::C version by about 20% (6.5s v 8) on my system when compiled with /Ox. Thank you.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

        Slightly faster. It's getting too silly, though. However, the step function is getting smaller due to better understanding of the problem.

        // inc_c - http://perlmonks.org/?node_id=1128230 // 8.52 secs. for 16/32 count=601080390 -O2 // 3.08 secs. for 16/32 count=601080390 -O3 #include <stdlib.h> #include <stdio.h> #define N 16 // number of elements wanted #define M 32 // place static int place[N+1]; static int count = 0; int step(void) { int *p = place; for(int i = 0; i < N; *p++ = i++ ) { if(*p < p[1] - 1) { ++*p; return 1; } } return 0; } int main(int argc, char **argv) { int i; int more = 1; for(i = 0; i < N; i++) place[i] = i; place[N] = M; while( more ) { //for(i = 0; i < N; i++) printf(" %d", place[i]); //putchar('\n'); count++; more = step(); } printf("\ncount %d\n", count); exit(0); }

        Thanks for the opportunity to scrape some rust off my C skills :)

        I'm still tweaking it - there may be more to come :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1128318]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-03-28 20:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found