Looking for replacement for clock() function

In summary, the conversation discusses an issue with a function not working correctly under Wine on a Linux system, and the need for a more accurate function with a higher frequency of changes. The possibility of compiling under Linux is mentioned, but the need for it to work under Wine is emphasized. A suggestion is made to use the boost libraries to create a boost::timer object for accurate timing.
  • #1
engri
4
0
This function seems to be working incorrectly when I run my programm under wine on linux system. (program compiled with mingw32 on win32) Is there any other function that can give me more accuracy than 1 second? (i know time() is working well but i need something that changes a little bit more frequent) Yes. I know I could compile it under linux but I need it to work under Wine right now.
 
Technology news on Phys.org
  • #2
ok never mind. i included this function and now it works fine both under win32 and linux:

http://www.openasthra.com/c-tidbits/gettimeofday-function-for-windows/
 
Last edited by a moderator:
  • #3
engri said:
This function seems to be working incorrectly when I run my programm under wine on linux system. (program compiled with mingw32 on win32) Is there any other function that can give me more accuracy than 1 second? (i know time() is working well but i need something that changes a little bit more frequent) Yes. I know I could compile it under linux but I need it to work under Wine right now.


You could always use the boost libraries to instantiate a boost::timer object.

Code:
#include <boost/timer.hpp>

...


// Create a boost::timer object
boost::timer timerObject;

// Something you want to time goes here
// ...

// Now calculate the elapsed time
std::cout << "Elapsed time = " << timerObject.elapsed << " sec.\n";
 

1. What is the purpose of the clock() function?

The clock() function is used to measure the amount of time that has passed since the program started running. It is often used for timing operations or to monitor the performance of a program.

2. Why would someone need to find a replacement for the clock() function?

There are several reasons why someone might need to find a replacement for the clock() function. One common reason is that the clock() function does not have a high enough resolution for certain applications. Additionally, the clock() function may not be available on all operating systems or may not be compatible with certain programming languages.

3. What are some alternatives to the clock() function?

Some alternatives to the clock() function include the gettimeofday() function, which provides a higher resolution timer, and the C++11 <chrono> library, which offers more advanced timing functionality. Other options include using the system clock or implementing a custom timer using low-level system calls.

4. How do I choose the best replacement for the clock() function?

The best replacement for the clock() function will depend on your specific needs and the requirements of your program. Consider factors such as resolution, compatibility, and ease of use when evaluating alternatives. It may also be helpful to test different options to determine which one works best for your particular situation.

5. Are there any potential drawbacks to using a replacement for the clock() function?

While there are many alternatives to the clock() function, each one may have its own drawbacks. For example, some alternatives may not be available on all platforms or may require more complex code to implement. It is important to carefully consider the pros and cons of each option before choosing a replacement for the clock() function.

Similar threads

  • Programming and Computer Science
Replies
4
Views
381
Replies
6
Views
1K
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
30
Views
4K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
12
Views
1K
  • Other Physics Topics
Replies
5
Views
1K
  • Programming and Computer Science
Replies
12
Views
6K
  • Programming and Computer Science
Replies
3
Views
307
Back
Top