Thread: calculation timer problem

  1. #1
    Registered User
    Join Date
    Feb 2012

    calculation timer problem

    I am trying to write a program that determines how many calculations it would take to find the determinant of a matrix, and how long it would take to do those calculations. I keep getting 0 seconds for an output. Can anybody see where I may have gone wrong?
    double mults_for_det(int n)
        if(n == 1)
            return 0;
            return n + n * mults_for_det(n - 1);
    int main(void)
        int n;
        double mults;
        clock_t start, end;
        printf("Enter a number for an nxn matrix.\n");
        start = clock();
        mults = mults_for_det(n);
        end = clock();
        printf("For n = %d, the %g multiplications take %g seconds.\n",n,
        mults,(double)(end - start) / (double)CLOCKS_PER_SEC);
        return 0;

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Print out CLOCKS_PER_SEC. If it's only 1000 (or even 1000000) it's probably not enough precision to measure one execution of your subroutine. Put it in a loop and execute it a million times. Then divide the clock difference by CLOCKS_PER_SEC and 1000000.

    Also, you need to change the input of your subroutine to double.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Dec 2011
    Oogabooga's solution works, in addition, if you're using x86, you could always use the timestamp counter:
    #define rdtscl(val) asm volatile ("rdtsc" : "=A" (val) : : );
    /* ... */
    unsigned long t1, t2;
    mults = mults_for_det(n);
    printf("Cycles = %lu\n", t2 - t1);
    But note that the count is not representative of any accurate real-time measure, it's mainly useful for comparing functions in optimisation. And don't expect accuracy over ~20 cycles, because so many factors can influence the time readings.

    You can *estimate* how long it takes by assuming that 1 cycle is approximately (1/c) nanoseconds, where c is the CPU frequency of your computer, in gHz. For example, running a 3.06gHz machine, a cycle is approximately 0.32 nanoseconds.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calculation problem
    By kburyanek in forum C Programming
    Replies: 8
    Last Post: 10-15-2009, 07:41 AM
  2. C# calculation problem
    By Diablo02 in forum C# Programming
    Replies: 13
    Last Post: 10-25-2007, 08:42 PM
  3. Calculation problem....
    By badran in forum C++ Programming
    Replies: 6
    Last Post: 10-17-2006, 04:07 AM
  4. Double calculation problem
    By Coconut in forum C Programming
    Replies: 6
    Last Post: 10-08-2002, 07:38 PM
  5. HELP!!! Calculation problem!
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2002, 11:09 AM

Tags for this Thread