From: Ilmari Heikkinen Date: 2007-09-25T08:50:33+09:00 Subject: Re: I am new to Ruby and I could use some expert advice as to how I can make this code run faster. On 9/24/07, Ilmari Heikkinen wrote: > On 9/24/07, Joel VanderWerf wrote: > > On 9/24/07, Ilmari Heikkinen wrote: > > ... > > > And here's a C version: > > > > To give C its fair due, you might try with > > > > unsigned long *buf; > > > > and > > > > buf[i] |= 0x80808080; > > > > (and the other necessary changes, of course). > > > And a version with 64-bit ints was even faster. But it had a bug too. > > Anyone wish to contribute working versions :)? Guess not, so here's one: $ time ./scramble64 trace trace.scramblec real 0m0.022s user 0m0.004s sys 0m0.020s $ export RUBYOPT= # -rubygems takes ~40ms $ time ruby -rscramble -e 'scramble_nobu("trace")' real 0m0.049s user 0m0.016s sys 0m0.032s $ diff trace.scramble* $ $ cat scramble64.c #include #include #include #include #include #define BUF_SIZE 4096 int main(int argc, char** argv) { int i, j, k, rsz, wsz; FILE *in_f, *out_f; unsigned long long isz; struct stat st; long long buf[BUF_SIZE]; if (argc != 3) { printf("USAGE: %s INFILE OUTFILE\n", argv[0]); return 1; } isz = sizeof(long long); in_f = fopen(argv[1], "r"); if (NULL == in_f) { printf("Failed to open input file %s\n", argv[0]); return 2; } out_f = fopen(argv[2], "w"); if (NULL == out_f) { printf("Failed to open output file %s\n", argv[1]); fclose(in_f); return 4; } fstat(fileno(in_f), &st); for(i=0,j = st.st_size / (isz*BUF_SIZE); i