LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Still need help with timer (https://www.linuxquestions.org/questions/programming-9/still-need-help-with-timer-549047/)

davidguygc 04-25-2007 08:44 PM

Still need help with timer
 
I posted a thread earlier, then said I figured it out. Well, no I didn't lol.

I need a timer to time how long a function takes to complete. My teacher thinks that milliseconds is enough time, but I think that the function completes in < 1 millisecond. So I need to find a function that returns time in micro or nano seconds. I'm doing this in C, on Windows (usually I'd say Linux, but my teacher won't accept Linux executables.)

davidguygc 04-25-2007 08:50 PM

This is my code:
Code:

/*****************************************************************/
/* CS 2350 Spring 2007                                                                                        */
/* C section for Assignment 5                                    */
/************************ include files **************************/
//65-90 97-122
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//#define CLOCKS_PER_SEC 1000

//External functions/variables
extern "C" void lexsort(char*,char*);
//Global functions/variables
int COUNT;
int ITER  = 0;

int main(void)
{
        putchar('\n');
        putchar(':');
        scanf("%d", &COUNT);
        //random-related statements/variables
        srand(time(0));
        int ran;

    char* input = new char[COUNT+1];
        char* output = new char[COUNT+1];       
        output[COUNT] = NULL;
        input[COUNT] = NULL;

        //time-related variables
        struct timeval before, after;
        double diff;
        int x = 0;

        while(x < COUNT)
        {
                ran = rand() % 57 + 65;
                if(ran > 90 && ran < 122)
                        continue;
                else
                {
                        input[x++] = ran;
                }
        }

        printf("input = %s\n\n", input);
        //implement lexsort function

        gettimeofday(&before, 0);
        lexsort(input, output);
        gettimeofday(&after, 0);
        //diff = after.tv_nsec - before.tv_nsec;
       
        //output results
        printf("output = %s\n", output);
        printf("Time to implement: %d", after.tv_usec);
        printf("\nbefore = %d", before.tv_usec);

        return 0;
}

Now, I really don't think my computer is fast enough to do a function in under 1 microsecond, (2.8 P4), but if thats the case, then I need a nanosecond timer.


All times are GMT -5. The time now is 10:32 AM.