Like Tree1Likes
  • 1 Post By Elysia

problem with time function

This is a discussion on problem with time function within the C++ Programming forums, part of the General Programming Boards category; Hey guys i am doing a basic swap program with generic pointers and I am trying to time it but ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    60

    problem with time function

    Hey guys i am doing a basic swap program with generic pointers and I am trying to time it but I keep getting 0. Any idea why?

    Code:
    
    
     #include <iostream>
     #include <iomanip>
    #include <ctime>
     using namespace std;
    
     void q(void *a, void *b, int n) {
    
    clock_t c0, c1;
    
    unsigned char *ca = (unsigned char *)a;
    unsigned char *cb = (unsigned char *)b;
    unsigned char c;
    
    c0 = clock();
    
    
         for (int i = 0; i < n; i++) {
             c     = ca[i];
             ca[i] = cb[i];
             cb[i] = c;
         }
    
    c1 = clock();
    
    cout << "Process time is " <<
          (double)(c1-c0)/CLOCKS_PER_SEC
          << " secs" << endl;
    
    
    
     }
    
     int main() {
    
         int a = 4, b = 7;
         float x = 2.5f, y = 3.5f;
    
         q(&a, &b, sizeof(int));
         q(&x, &y, sizeof(float));
    
         cout << setprecision(1) << fixed;
         cout << "(a,b) = (" << a << ',' << b << ')' << endl;
         cout << "(x,y) = (" << x << ',' << y << ')' << endl; 
     }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,272
    Probably because what you are timing completes faster than the resolution of the timer

    By the way, you should indent your code better
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    is there anything i can change so i can get a time?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,833
    Well if you're on windows, then read QueryPerformanceCounter function (Windows)
    Or state what machine/OS/Compiler you're using.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    17
    hey make your process to take more time for eg. put some sleep(2000) in for loop then you will get the time as 1.99999 like this...

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by ushacy View Post
    hey make your process to take more time for eg. put some sleep(2000) in for loop then you will get the time as 1.99999 like this...
    Doesn't really solve his problem as the OP was looking to check how long it took for his sort to work.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Another good idea might to run the algorithm for X iterations and time it. Then divide your time with the number of iterations and voila - you get a result. An average even, which is better statistically than a single iteration.
    AndrewHunter likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    basically what I want to do is take that swap function and make it happen 1000 times. I believe if I do that I should get a time value. I tried replacing n with 1000 but then it would just mess up the swap all together, so I have to keep sizeof.

    Is there anyway I could make the swap happen 1000 times while still using the sizeof I am sending in?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,833
    Where did 1000 come from?

    Elysia suggested this
    Code:
    - clock
    -- for ( i = 0 ; i < 100000 ; i++ )
    --- q(&a, &b, sizeof(int));
    -- end for
    - clock
    - print time for 1M swaps of an int
    Move the clock code from the function under test and put it in main.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 05-23-2011, 12:20 AM
  2. Time function
    By goran00 in forum C Programming
    Replies: 2
    Last Post: 01-21-2008, 02:56 PM
  3. Time Function
    By smegly in forum C Programming
    Replies: 3
    Last Post: 05-20-2004, 11:49 AM
  4. Replies: 1
    Last Post: 10-14-2002, 05:33 AM
  5. Time Function
    By KRonin4015 in forum C++ Programming
    Replies: 3
    Last Post: 05-28-2002, 07:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21