Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[C] time.h

  1. Oct 25, 2006 #1
    I've written an algorithm that has the following goal: finding all prime numbers up to a specified integer. I've made two different algorithms actually: on one hand, I've used the concept beyond the ancient sieve of eratosthenes; on the other, I've used a function called isprime() that tests if a number is prime. For instance, let's say I want to determine wich method is faster. So I need to time a piece of code in each algorithm.

    As an example, here's the code of the first algorithm:

    Code (Text):
    #include <stdio.h>
    #include <math.h>
    #include <time.h>

    int isprime(int num);

    int main () {
    int n, num;
    time_t t1, t2;

    printf("Enter a number\n");

    while (scanf("%d", &n) != 1) {
    printf("Error: Try again.\n");
        while (getchar() != '\n')

    printf("Prime numbers\n");


    for (num = 2; num <= n; num += 1) {
    if (isprime(num) == 1)
    printf("%d\n", num);


    printf("Time elapsed: %f ms\n", (float)(t2-t1));

    return 0;
    I've omitted the isprime() function definition, because I felt it's unnecessary. The problem with this approach is that my computer writes the numbers so rapidly that I probably will need to write the time in milliseconds if I wanted the algorithm to write every prime number up to a small number, as 11. Is there a function that allows me to do that?
    Thank you on advance.

    Links I've consulted

    "[URL [Broken]
    Last edited by a moderator: May 2, 2017
  2. jcsd
  3. Oct 25, 2006 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    There is the clock() function. But if you want really accurate timing, your best bet is to do one of the following: (I think (1) is the most reliable)

    (1) Wrap your code in a loop and repeat it a few thousand times (or million, as necessary), so that the elapsed time is measured in seconds or minutes.

    (2) Use a profiling tool.

    (3) Try and find an API for the high-performance timer provided by your operating system.

    Incidentally, there are two problems with your code:
    (1) Printing the primes to screen is probably more time-consuming than the calls to isprime().

    (2) On the final printf(...) call, you told it to print an int value (%d), but passed it a float.
  4. Oct 25, 2006 #3
    Note though that the clock() function measures the cpu time not the elapsed time.
  5. Oct 25, 2006 #4
    Sorry for that, didn't notice.

    About the clock() function I will try to use it. I found an interesting thread about that function http://www.thescripts.com/forum/thread213478.html" [Broken]. When I come up with a fully functional solution I will post it. The (3) solution will also be considered. I didn't know about the existence of 'profiling tools', so I just learned something. Thank you for the suggestions.
    Last edited by a moderator: May 2, 2017
  6. Oct 26, 2006 #5
    you have figured out by now, if not maybe this can help

    Code (Text):

    #include <ctime>

    clock_t cstart = clock();
    clock_t cend = clock();

    double millis = 1000.0 * (cend - cstart) / CLOCKS_PER_SEC;

  7. Oct 26, 2006 #6
    The clock() function returns, as MeJennifer stated before, the cpu time which is less than the elapsed time (recall Hurkyl's post: 'Printing the primes to screen is probably more time-consuming than the calls to isprime()'). So I tried this solution, which seems to work although it's not extremely accurate, as one could desire (nor is the clock() function: it returns 0.00000 s cpu time if the computation is done too fast):

    Code (Text):
    #include <sys/times.h>
    #include <sys/wait.h>

      static clock_t st_time;
      static clock_t en_time;
      static struct tms st_cpu;
      static struct tms en_cpu;

      st_time = times(&st_cpu);
      en_time = times(&en_cpu);

    printf("Time elapsed: %ld ms\n", (en_time - st_time));
    Example: Print all primes up to 200.
    Cpu_time: 0.000000 s
    Time elapsed: 0 s (using <time.h>)
    Time elapsed: 15 ms (using struct)

    http://www.codeproject.com/csharp/highperformancetimercshar.asp" [Broken]
    Last edited by a moderator: May 2, 2017
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook