MMM
Results 1 to 25 of 815

Thread: New Multi-Threaded Pi Program - Faster than SuperPi and PiFast

Threaded View

  1. #11
    Xtreme Addict
    Join Date
    Apr 2007
    Posts
    2,128
    By performance timers do you mean getting the time delta from the start of the run and the end of the run? If so, don't use ctime/time.h for gathering the elapsed time, it is inaccurate. Instead, you have to use <sys/time.h> in linux to gather the elapsed time.

    The way how it works:

    Code:
    #include <iostream>
    #include <sys/time.h>
    
    
    void init_timer(timeval &tv) {
        /* Update tv to the current time */
        gettimeofday(&tv, 0);
    };
    
    unsigned int get_time(timeval &start, timeval &end) {
        /* Update end to current time, at this point start should be updated earlier by init_timer() */
        gettimeofday(&end, 0);
        /* Calculate the difference in microseconds(tv_usec) and convert it to milliseconds, do the same to seconds(tv_sec) */
        return (end.tv_usec-start.tv_usec)/1000 + (end.tv_sec-start.tv_sec)*1000;
    };
    
    int main() {
    
        timeval tv_start;
        timeval tv_end;
    
        init_timer(tv_start);
        /* Do something */
        std::cout << get_time(tv_start, tv_end) << std::endl;
        return 0;
    };
    timeval structure and gettimeofday(timeval &) gives microsecond accuracy, which is then truncated to milliseconds which should be precise enough. At least has been for my use.

    The good thing about sys/time.h is that it is part of C POSIX library, so it should work with any POSIX compicant operating systems.
    Last edited by Calmatory; 08-05-2010 at 11:42 AM.

Tags for this Thread

Bookmarks

Bookmarks

Posting Permissions

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