| |
![]() Sama Suomeksi Front Page Overview Schedule Lectures Guides Topics Teachers |
T-106.290 Ohjelmoinnin laboratoriotyötHow to Measure CPU Time.This document gives a few tricks of how to measure time.
The
Here's a sample program of how to use
#include <stdio.h>
#include <unistd.h>
#include <sys/times.h>
double give_time(void)
{
struct tms tms;
long ticks_per_second, ticks;
ticks = times(&tms);
if (ticks == -1) {
fprintf(stderr, "'times' failed\n");
exit(1);
}
ticks_per_second = sysconf(_SC_CLK_TCK);
if (ticks_per_second == -1) {
fprintf(stderr, "'sysconf(_SC_CLK_TCK)' failed.\n");
exit(1);
}
return ticks / (double) ticks_per_second;
}
int fib(int n)
{
return n < 2 ? 1 : fib(n-1) + fib(n-2);
}
int main(int argc, char **argv)
{
int f;
double start_time;
start_time = give_time();
printf("fib(40) = %d\n", fib(40));
printf("duration = %f seconds\n", give_time() - start_time);
return 0;
}
When executed the output code look something like this:
fib(40) = 165580141
duration = 12.340000 seconds
The main drawback of
#include <stdio.h>
#ifdef __GNUC__
#ifdef __i386__
/* Requires a Pentium or newer. */
#define TICKS(t) asm volatile ("rdtsc" : "=a" (t) : : "edx")
#endif
#ifdef __sparc_v9__
/* The #ifdef above is not fully reliable. Please pass
`-mcpu=ultrasparc' to the compiler or `-xarch=v8plusa' to the
assembler. */
#define TICKS(t) asm volatile("rd %%tick,%0" : "=r" (t))
#endif
#ifdef PPC
#define TICKS(t) asm volatile("mfspr %0,268" : "=r" (t))
#endif
#endif /* __GNUC__ */
#ifndef TICKS
#define TICKS(t) do { (t) = 0; } while (0)
#endif
int fib(int n)
{
return n < 2 ? 1 : fib(n-1) + fib(n-2);
}
int main(int argc, char **argv)
{
int f;
unsigned long start_time, end_time;
TICKS(start_time);
f = fib(4);
TICKS(end_time);
printf("fib(4) = %d\n", f);
printf("duration = %lu ticks\n", end_time - start_time);
return 0;
}
Unfortunately the value assigned by
Note also that the value assigned by |
Course email: cessu@cs.hut.fi
Kurssin newsgroup: opinnot.tik.labratyot
This page has been last updated on 2005-01-11.