Oct:31(Fri)
Consider the following six program fragments:
sum = 0;
for ( i = 0; i < n; i++ )
for ( j = 0; j < 100; j++ )
sum++;
sum = 0;
for ( i = 0; i < n; i++ )
for ( j = 0; j < n; j++ )
sum++;
sum = 0;
for ( i = 0; i < n; i++ )
for ( j = 0; j < n * n; j++ )
sum++;
sum = 0;
for ( i = 0; i < n; i++ )
for ( j = 0; j < i; j++ )
sum++;
sum = 0;
for ( i = 0; i < n; i++ )
for ( j = 0; j < i * i; j++ )
for ( k = 0; k < j; k++ )
sum++;
sum = 0;
for ( i = 1; i < n; i++ )
for ( j = 1; j < i * i; j++ )
if ( j % i == 0 )
for ( k = 0; k < j; k++ )
sum++;
n
as a command-line argument
(rem. in Eclipse use Run...),
and output the final value of sum.
n
as follows:
time java program_name value_for_n
And among the three times reported by time,
the one that is best for our purposes here is
the one labeled "user".
n,
try powers of ten —
1,10,100,1000,10000,... —
until the time of execution is over a minute.
r_n —
i.e. values of the form (r_n)k —
that are closest to the ratios between times.
values ratios
used of n-s ratios values (r_n)k
for n r_n times of times closest to ratios of times
----- ------ -------- -------- --------------------------
100 N.A. 0.01s. N.A. N.A.
200 2 0.13s. 13.00 16 == 24
400 2 0.99s. 7.61 8 == 2³
800 2 7.59s. 7.67 8 == 2³
1600 2 61.34s. 8.08 8 == 2³
[Acknowledgement: this exercise is derived from Mark Allen Weiss.]