problem with clock()
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: problem with clock()

  1. #1
    Join Date
    Apr 2011
    Posts
    16

    problem with clock()

    I want to calculate the time elapsed since the execution of take_forks() but the value of "b" and "end"are always 0.Why this happens?


    Code:
    clock_t start,end;
    	start=clock();//clocks before taking the forks
    	take_forks(i);
    	end=clock();//clocks after taking forks
    	
    	
            double b=(double)(end-start)/(double)(CLOCKS_PER_SEC);
    
    tmesos=tmesos+(b/counter)/n;
    printf("waiting time is %f",b);

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,606

    Re: problem with clock()

    And what is the value of start?
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2009
    Posts
    49

    Re: problem with clock()

    CLOCKS_PER_SEC is usually 1000 on most systems. Perhaps the code you benchmarked ran fast enough and took less than 1ms?

  4. #4
    Join Date
    Apr 2011
    Posts
    16

    Re: problem with clock()

    The value of start is also 0.On failure clock() returns -1 thus it did not failed. Is there another way to benchmark my function?

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: problem with clock()

    Quote Originally Posted by killbill689 View Post
    The value of start is also 0.On failure clock() returns -1 thus it did not failed. Is there another way to benchmark my function?
    On Windows, QueryPerformanceFrequency() and QueryPerformanceCounter(). On Linux and similar, gettimeofday(). I think gettimeofday() works on OSX too but I'm not sure.

    These functions have better resolution than clock().

  6. #6
    Join Date
    Apr 2011
    Posts
    16

    Re: problem with clock()

    I used gettimeofday() but i have the same problem
    Code:
    struct timeval start,end;
    
            gettimeofday(&start,NULL);
    	take_forks(i);
    	gettimeofday(&end,NULL);
    
    double b=(double)(end.tv_usec-start.tv_usec);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center