Well that's a weird result BUK :) So I actually compiled your code (with some changes to make it suitable for gcc on linux). I was getting strange results too. 1500 nanoseconds for anonM algorithm, no matter what s was (at -O2). I'm curious why that happened. With some checksums to verify results the execution time became more realistic (with 1:10 bias towards bitshift stuff, but perhaps your compiler recognizes Euclid's algorithm? gcc 4.7 doesn't, it seems :) I don't know C very well, though (so here it is)
#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <inttypes.h> #include <time.h> typedef uint64_t U64; struct timespec diff_time( struct timespec end, struct timespec start ) { struct timespec res; res.tv_sec = end.tv_sec - start.tv_sec; res.tv_nsec = end.tv_nsec - start.tv_nsec; if ( res.tv_nsec < 0 ) { res.tv_nsec += 1000 * 1000 * 1000; res.tv_sec -= 1; } return res; } U64 gcd( U64 a, U64 b ) { return !b ? a : gcd( b, a % b ); } U64 lcm( U64 a, U64 b ) { return a * b / gcd( a, b ); } U64 gcm( U64 max, U64 n ) { U64 c, lcm; for( c = 1; ( ~n & 1 ) && ( c < 4096 ); c <<= 1 ) n >>= 1; lcm = n * 4096; return ( max / lcm ) * lcm; } #define GB ( 1024ull * 1024ull * 1024ull ) #define PAGE 4096 int main(int argc, char **argv) { U64 r, chk; U64 s = argc > 1 ? strtoull( argv[1], NULL, 10 ) : GB; struct timespec start, end, difft; clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &start ); for( chk = 0, r = 1; r < ( s >> 1 ); ++r ) { chk += gcm( s, r ); } clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &end ); difft = diff_time( end, start ); printf( "anonyM: gcm for s=%" PRIu64 " & r=1 to %" PRIu64 " took:%lld secs %ld nsecs\n" "chk = %" PRIu64 "\n", s, s>>1, (long long) difft.tv_sec, difft.tv_nsec, chk ); clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &start ); for( chk = 0, r = 1; r < ( s >> 1 ); ++r ) { chk += s - ( s % lcm( r, 4096 ) ); } clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &end ); difft = diff_time( end, start ); printf( "oiskuu: gcm for s=%" PRIu64 " & r=1 to %" PRIu64 " took:%lld secs %ld nsecs\n" "chk = %" PRIu64 "\n", s, s>>1, (long long) difft.tv_sec, difft.tv_nsec, chk ); return 0; }

In reply to Re^2: Simple arithmetic? (And the winner is ... ) by Anonymous Monk
in thread Simple arithmetic? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.