The three routines gcd1, gcd2, gcd3 were run on successively larger inputs (i,j). For each run, there is a point displayed in the plot: (i, time(i,j)). (In log-log scale). where time(i,j) is the number of milliseconds for the routine to run on input i,j. Red points are for gcd1, green for gcd2, blue for gcd3.
We can see that gcd1 takes time linear in i, whereas gcd2 and gcd3 are running much faster (typically in under a millisecond). Gcd2 was unable to handle some large inputs because of memory usage (the stack takes size proportional to i).
The following routine was used to call gcd1, gcd2, gcd3 on multiple inputs:
#include <iostream> #include <stdlib.h> #include "timer.h" main(argc, *argv[]) { int which = atoi(argv[1]); for (int t = 1; elapsed_msec() < 5000; ++t) { float time = elapsed_msec(); int i = t * 100000; if (i > 1<<30) break; int j = i/2 + (random() % i/2); switch(which) { case -1: gcd1(i,j); break; case -2: gcd2(i,j); break; case -3: gcd3(i,j); break; } cout << i << " " << elapsed_msec()-time << endl; } }The output was piped into three files data1, data2, data3, and the plot was generated with the gnuplot commands
set logscale xy set pointsize 0.2 plot [] [0.005:100] 'data1', 'data2', 'data3', log(x) with lines, x with lines