Skip to Content
C in a Nutshell
book

C in a Nutshell

by Peter Prinz, Tony Crawford
December 2005
Beginner to intermediate
618 pages
20h 19m
English
O'Reilly Media, Inc.
Content preview from C in a Nutshell

Name

clock

Synopsis

Obtains the CPU time used by the process

#include <time.h>
clock_tclock( void );

If you want to know how much CPU time your program has used, call the clock() function. The function’s return type, clock_t, is defined in time.h as long. If the function returns -1, then the CPU time is not available. Note that the value of clock() does not reflect actual elapsed time, as it doesn’t include any time the system may have spent on other tasks.

The basic unit of CPU time, called a “tick,” varies from one system to another. To convert the result of the clock() call into seconds, divide it by the constant CLOCKS_PER_SEC, which is also defined in time.h.

Example

#include <stdio.h>
#include <time.h>
time_t start, stop;
clock_t ticks; long count;

int main()
{time(&start);
  for (count = 0; count <= 50000000; ++count)
  {
    if (count % 1000000 != 0) continue;   /* measure only full millions */
    ticks = clock();
    printf("Performed %ld million integer divisions; "
           "used %0.2f seconds of CPU time.\n",  count / 1000000,
           (double)ticks/CLOCKS_PER_SEC);
  }
  time(&stop);
  printf("Finished in about %.0f seconds.\n", difftime(stop, start));
  return 0;
}

This program produces 51 lines of output, ending with something like this:

Performed 50 million integer divisions; used 2.51 seconds of CPU time.
Finished in about 6 seconds.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

C in a Nutshell, 2nd Edition

C in a Nutshell, 2nd Edition

Peter Prinz, Tony Crawford
Smaller C

Smaller C

Marc Loy
C Pocket Reference

C Pocket Reference

Peter Prinz, Ulla Kirch-Prinz
Extreme C

Extreme C

Kamran Amini

Publisher Resources

ISBN: 0596006977Errata Page