in reply to Re^4: Odometer pattern iterator (in C).
in thread Odometer pattern iterator (in C). (Updated.)
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); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^6: Odometer pattern iterator (in C).
by BrowserUk (Patriarch) on May 30, 2015 at 02:18 UTC | |
by Anonymous Monk on May 30, 2015 at 03:03 UTC | |
by BrowserUk (Patriarch) on May 30, 2015 at 08:13 UTC | |
by tybalt89 (Monsignor) on Dec 30, 2016 at 21:50 UTC | |
by Anonymous Monk on May 30, 2015 at 02:26 UTC |