473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Besides clock() is there another way to get the clock time?

Is there a way to get the clock time besides clock()?


Here's my specific problem:

I wrote a little "free camera" application in C++, using OpenGL and SDL.
Its camera was controlled with keyboard & mouse, like in a first-person shooter.
To see where I was going I had it draw a 50x50x50 cube of octahedra:
Expand|Select|Wrap|Line Numbers
  1. int i, j, k, l = 500, m = 80;  // l is the radius, m is the side length of the volume
  2. glBegin (GL_QUADS);
  3. for (i=0;  i<m;  i++)
  4. {
  5.     for (j=0;  j<m;  j++)
  6.     {
  7.         for (k=0;  k<m;  k++)
  8.         {
  9.             glColor3f (1, 0.5, 0);  // Draw a quad on the YZ plane
  10.             glVertex3f (i*10000, j*10000+l, k*10000);
  11.             glVertex3f (i*10000, j*10000, k*10000+l);
  12.             glVertex3f (i*10000, j*10000-l, k*10000);
  13.             glVertex3f (i*10000, j*10000, k*10000-l);
  14.             glColor3f (1, 0.6, 0);  // Draw a quad on the XY plane
  15.             glVertex3f (i*10000, j*10000+l, k*10000);
  16.             glVertex3f (i*10000+l, j*10000, k*10000);
  17.             glVertex3f (i*10000, j*10000-l, k*10000);
  18.             glVertex3f (i*10000-l, j*10000, k*10000);
  19.             glColor3f (1, 0.7, 0);  // Draw a quad on the XZ plane.  Together they look look an octahedron
  20.             glVertex3f (i*10000+l, j*10000, k*10000);
  21.             glVertex3f (i*10000, j*10000, k*10000+l);
  22.             glVertex3f (i*10000-l, j*10000, k*10000);
  23.             glVertex3f (i*10000, j*10000, k*10000-l);
  24.         }
  25.     }
  26. }
  27. glEnd();
  28.  
This creates 6.1M verteces, which runs pretty slowly on my computer, so I tried putting it in a display list.

To make the camera move at the same speed regardless of framerate I figured this out: At the end of each frame I take the number of seconds since the last frame frame:
Expand|Select|Wrap|Line Numbers
  1. //  double CT is the Current Time
  2. //  double LT is the Last frame's Time
  3. //  double DT is the Difference in Time between this frame and the last
  4. LT = CT;
  5. CT = clock();
  6. if (LT)  DT = CT - LT;  // LT is assigned zero at the beginning of the program.  This way there's no time for the first frame.
  7. DT /= CLOCKS_PER_SEC;
  8. cout << CT << ' ' << DT << '\n';
  9.  
Then I scale the camera movement by that amount:
Expand|Select|Wrap|Line Numbers
  1. //  MFore = 1 means the "foreward" key is down
  2. //  Player.Pos is the player's location
  3. //  Player.AimX and AimY indicate where the player is aiming
  4. //  MSpeed is the movement speed in OpenGL units/second
  5. if (MFore)
  6. {
  7.     Player.Pos.X += (int) (sin(Player.AimX) * cos(Player.AimY) * MSpeed * DT);
  8.     Player.Pos.Y += (int) (sin(Player.AimY) * MSpeed * DT);
  9.     Player.Pos.Z += (int) (cos(Player.AimX) * cos(Player.AimY) * MSpeed * DT);
  10. }
  11.  
When I ran it without a display list the console looked like this:
Expand|Select|Wrap|Line Numbers
  1. 340000 0
  2. 590000 0.25
  3. 840000 0.25
  4. 1.09e+06 0.25
  5. 1.34e+06 0.25
  6.  
I get around four frames per second, on average. This is what it's supposed to look like, and everything was drawn and viewed properly.

When I run it using a display list to draw the octahedra it ends up looking like this:
Expand|Select|Wrap|Line Numbers
  1. 0 0
  2. 440000 0
  3. 440000 0
  4. 440000 0
  5. 440000 0
  6. 440000 0
  7. 440000 0
  8.  
Since it always thinks the frame time is zero, the camera doesn't move.

So I'm looking for a more precise, or just different way to get the framerate or clock time. Any ideas?


Written in C++ under Ubuntu 7.10 running on Intel Pentium 4 at 2.66GHz and ATI Radeon 9550SE (256MB).
Using OpenGL and SDL
Dec 29 '07 #1
2 1679
Savage
1,764 Expert 1GB
So I'm looking for a more precise, or just different way to get the framerate or clock time. Any ideas?



Google for QueryPerformanceFrequency and QueryPerformanceCounter,they together form a very precise counter,which error is about 10e-5 secounds.
Dec 30 '07 #2
Google for QueryPerformanceFrequency and QueryPerformanceCounter,they together form a very precise counter,which error is about 10e-5 secounds.
I'm running Linux, and according to Answers.com QueryPerformanceCounter is a Windows thing.
I found a function called ftime (timeb*), which is the closest thing for Linux. It offers millisecond timing and is far more reliable than clock().

Thanks anyway, I couldn't have found the Linux equivalent if I didn't have the Windows one in the first place!
Jan 5 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: James Harriman | last post by:
Hi, I need to be able to measure a time interval in milliseconds on a windows machine. I have tried using time.clock() but it appears to measure time in seconds...Is there a way to measure time...
13
by: Peter Hansen | last post by:
I would like to determine the "actual" elapsed time of an operation which could take place during a time change, in a platform-independent manner (at least across Linux/Windows machines). Using...
8
by: peterbe | last post by:
What's the difference between time.clock() and time.time() (and please don't say clock() is the CPU clock and time() is the actual time because that doesn't help me at all :) I'm trying to...
0
by: Peger, Daniel H. | last post by:
Hi, I'm having a slight problem with the precision of the standard c++ clock function as it is defined in time.h . On my system the shortest meassurable time period is 0.01 seconds, but for my...
33
by: Pushkar Pradhan | last post by:
I'm using clock() to time parts of my code e.g. clk1 = clock(); /* code */ clk2 = clock(); /* calculate time in secs */ ...... clk1 = clock(); /* code */ clk2 = clock();
54
by: CoreyWhite | last post by:
The following experiment is a demonstration of TIME TRAVEL. When writing this program, and testing it out I found that sometimes the program would engage itself in time travel but other times it...
9
by: Ron Adam | last post by:
I'm having some cross platform issues with timing loops. It seems time.time is better for some computers/platforms and time.clock others, but it's not always clear which, so I came up with the...
5
by: Charles May | last post by:
Anyone have a simple concept for the best way to store timeclock information in a database. I currently have my table set up like this with a typical daily entry. tcID empID Type ...
8
by: Theo v. Werkhoven | last post by:
hi, In this code I read out an instrument during a user determined period, and save the relative time of the sample (since the start of the test) and the readback value in a csv file. #v+...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.