time.h

Hi guys

I have a question.
I have some sorting functions and i want to count the time for each function to complete the sorting.

i know that i must use time_t in
but i dont know how....
can you help?

Comments

  • [b][red]This message was edited by stober at 2004-10-30 13:36:7[/red][/b][hr]
    : Hi guys
    :
    : I have a question.
    : I have some sorting functions and i want to count the time for each function to complete the sorting.
    :
    : i know that i must use time_t in
    : but i dont know how....
    : can you help?
    :


    you probably can't use the functions in time.h because they are only accurate to the second. You need something with a lot more precision then that. If you are using Windows console or GUI you can use the Win32 API function GetTickCount() which returns the time in milliseconds, or QueryPerformanceCounter(). Call it just before starting the sort function and again afterwards, then subtract the two values to get elapsed number of milliseconds. If the difference is 0, then that means it took less than 1 millisecond to sort the data. Either run the sort function more often or increase the amount of data to get a measurable time. For example you might want to sort the same set of data 1,000 times.


  • Would this be possible?

    [code]long long timer;

    asm
    {
    rdtsc
    mov dword ptr timer, eax
    mov dword ptr timer[4], edx
    }[/code]
  • : [b][red]This message was edited by stober at 2004-10-30 13:36:7[/red][/b][hr]
    : : Hi guys
    : :
    : : I have a question.
    : : I have some sorting functions and i want to count the time for each function to complete the sorting.
    : :
    : : i know that i must use time_t in
    : : but i dont know how....
    : : can you help?
    : :
    :
    :
    : you probably can't use the functions in time.h because they are only accurate to the second. You need something with a lot more precision then that. If you are using Windows console or GUI you can use the Win32 API function GetTickCount() which returns the time in milliseconds, or QueryPerformanceCounter(). Call it just before starting the sort function and again afterwards, then subtract the two values to get elapsed number of milliseconds. If the difference is 0, then that means it took less than 1 millisecond to sort the data. Either run the sort function more often or increase the amount of data to get a measurable time. For example you might want to sort the same set of data 1,000 times.
    :
    :
    :
    This is an exercise and it must be compatible in Solaris or Sun/OS
    I do my homework at home with a win compiler (JFE with gcc) but i must use time_h to be compatible with unix operating systems :).

    So before call i.e. bubble_sort();
    i must call: time_h start=time(NULL);
    and..
    after the call: time_h end=time(NULL);

    then printf("time=%ld",end-start);
    is this correct?

  • : Would this be possible?
    :
    : [code]long long timer;
    :
    : asm
    : {
    : rdtsc
    : mov dword ptr timer, eax
    : mov dword ptr timer[4], edx
    : }[/code]
    :

    no, because assembly language is not portable to other operating systems.
  • [b][red]This message was edited by stober at 2004-10-30 15:12:21[/red][/b][hr]
    : This is an exercise and it must be compatible in Solaris or Sun/OS
    : I do my homework at home with a win compiler (JFE with gcc) but i must use time_h to be compatible with unix operating systems :).
    :
    : So before call i.e. bubble_sort();
    : i must call: time_h start=time(NULL);
    : and..
    : after the call: time_h end=time(NULL);
    :
    : then printf("time=%ld",end-start);
    : is this correct?
    :
    :

    like I said, the functions in time.h can't be used because the result time will be 0. But if you insist, your example is not correct
    [code]
    #include

    int main()
    {
    time_t start,end;
    start = clock();
    DoBubbleSort();
    end = clock();
    printf("diff: %d
    ", end-start);
    return 0;
    }
    [/code]


  • : [b][red]This message was edited by stober at 2004-10-30 15:12:21[/red][/b][hr]
    : : This is an exercise and it must be compatible in Solaris or Sun/OS
    : : I do my homework at home with a win compiler (JFE with gcc) but i must use time_h to be compatible with unix operating systems :).
    : :
    : : So before call i.e. bubble_sort();
    : : i must call: time_h start=time(NULL);
    : : and..
    : : after the call: time_h end=time(NULL);
    : :
    : : then printf("time=%ld",end-start);
    : : is this correct?
    : :
    : :
    :


    : like I said, the functions in time.h can't be used because the result time will be 0. But if you insist, your example is not correct
    : [code]
    : #include
    :
    : int main()
    : {
    : time_t start,end;
    : start = clock();
    : DoBubbleSort();
    : end = clock();
    : printf("diff: %d
    ", end-start);
    : return 0;
    : }
    : [/code]
    :
    :
    :

    Actually his method of using time() is much better than using clock() if 1 second resolution is ok.

    clock() should never be used for timing, because it only returns CPU time used by your process.. example: if you call sleep() inbetween clock() calls, the amount of time spent sleeping will not be included in the clock() return!

    Also, clock() returns time in CPU time, not in seconds. To convert the return to the number of seconds used, you need to do something like:

    time_t value = clock() / CLOCKS_PER_SEC;



    A better method to timing with microsecond accuracy, suitable for function timing would be to use gettimeofday() instead. It returns the time in seconds and microseconds.




    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

  • : : Would this be possible?
    : :
    : : [code]long long timer;
    : :
    : : asm
    : : {
    : : rdtsc
    : : mov dword ptr timer, eax
    : : mov dword ptr timer[4], edx
    : : }[/code]
    : :
    :
    : no, because assembly language is not portable to other operating systems.
    :


    Heres an assembly rdtsc that works for linux (gcc), for pentium processors..
    [code]
    // For Pentiums, you can get the number of clock cycles elapsed since the last reboot
    // with the following C code (which executes the CPU instrution named RDTSC)

    __inline__ unsigned long long int rdtsc()
    {
    unsigned long long int x;
    __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
    return x;
    }
    [/code]
    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

  • : [code]
    : // For Pentiums, you can get the number of clock cycles elapsed since the last reboot
    : // with the following C code (which executes the CPU instrution named RDTSC)
    :
    : __inline__ unsigned long long int rdtsc()
    : {
    : unsigned long long int x;
    : __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
    : return x;
    : }
    : [/code]
    :


    The op wants something that's compatible with Sun

    http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=3&MsgID=279628&Setting=A9999F0001

  • : : [code]
    : : // For Pentiums, you can get the number of clock cycles elapsed since the last reboot
    : : // with the following C code (which executes the CPU instrution named RDTSC)
    : :
    : : __inline__ unsigned long long int rdtsc()
    : : {
    : : unsigned long long int x;
    : : __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
    : : return x;
    : : }
    : : [/code]
    : :
    :
    :
    : The op wants something that's compatible with Sun
    :
    : http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=3&MsgID=279628&Setting=A9999F0001
    :
    :
    thanks guys clock () is ok
  • [b][red]This message was edited by dionys at 2004-10-31 2:52:45[/red][/b][hr]
    [b][red]This message was edited by dionys at 2004-10-31 2:51:21[/red][/b][hr]
    [CODE]
    #include
    #include
    #include
    int binsearch(int a[],int low, int high, int target)
    {
    int mid;

    if (low == high)
    return -1;
    else {
    mid = (high + low)/2;
    if (a[mid] == target)
    return(mid);
    else
    if (target < a[mid])/* is target in bottom half of a? */
    return(binsearch(a,low,mid-1,target));
    else
    return(binsearch(a,mid+1,high,target));
    }
    }
    int LinearSearch(int a[],int low, int high, int target){
    int i;

    for(i=low; i<high; i++){
    if(a[i]==target)
    return i;
    }
    return -1;
    }
    int main (void) {

    int array[10000],i;
    clock_t start,stop;
    srand(time(NULL));

    for(i=0; i<10000; i++)
    array[i]=rand()%1000+1;

    start = clock ();
    binsearch(array,0,10000,-10001);
    stop = clock ();
    printf("stop=%lf
    ",stop);
    printf("Runtime = %.18lf

    ",((double) stop - start) / (double) CLOCKS_PER_SEC);
    return 0;
    }
    [/CODE]

    i take 0.000000 for stop-start
    stop is also 0 why?
    what is wrong with this?




  • : [b][red]This message was edited by stober at 2004-10-30 13:36:7[/red][/b][hr]
    : : Hi guys
    : :
    : : I have a question.
    : : I have some sorting functions and i want to count the time for each function to complete the sorting.
    : :
    : : i know that i must use time_t in
    : : but i dont know how....
    : : can you help?
    : :
    :
    :
    : you probably can't use the functions in time.h because they are only accurate to the second. You need something with a lot more precision then that. If you are using Windows console or GUI you can use the Win32 API function GetTickCount() which returns the time in milliseconds, or QueryPerformanceCounter(). Call it just before starting the sort function and again afterwards, then subtract the two values to get elapsed number of milliseconds. If the difference is 0, then that means it took less than 1 millisecond to sort the data. Either run the sort function more often or increase the amount of data to get a measurable time. For example you might want to sort the same set of data 1,000 times.
    :
    :
    : i think the best result is call your method sevaral times and then divide the time elapsed to your number of calling method



  • [b][red]This message was edited by stober at 2004-10-31 5:42:57[/red][/b][hr]
    : i take 0.000000 for stop-start
    : stop is also 0 why?
    : what is wrong with this?
    :

    [red]read my previous response -- i already answered that question[/red]

    http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=3&MsgID=279623&Setting=A9999F0001


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories