Up: Date And Time Library Samples   [Index]


7.13.3.1 Timing A Task

Time the insertion of 1048576 numbers in an empty list, result on standard error.

date f, s;
list l;
integer i;

# get current time
d_now(s);

i = 0;
while (i < (1 << 20)) {
    # insert _i_ in a random position (0 .. i)
    l_p_integer(l, drand(i), i);
    i += 1;
}

# get current time
d_now(f);

if (1) {
    file e;
    time t;

    # compute the difference
    t_ddiff(t, f, s);

    f_open(e, "/dev/stderr", OPEN_WRITEONLY, 0);

    f_integer(e, t_second(t));
    f_byte(e, '.');
    f_bfxinteger(e, 0, 2, 10, t_microsecond(t) / 10000);
    f_byte(e, 10);

    f_close(e);
}