SFML community forums

Help => System => Topic started by: Redee on November 25, 2014, 06:20:44 pm

Title: Time algorithms C++ ?
Post by: Redee on November 25, 2014, 06:20:44 pm
How to measure time bases on machine physical events which will be constant exact at different PCs ?
Title: Re: Time algorithms C++ ?
Post by: Nexus on November 25, 2014, 06:28:15 pm
What do you mean? In SFML, there is sf::Clock, which works portably.
Title: Re: Time algorithms C++ ?
Post by: Redee on November 25, 2014, 06:47:37 pm
Me interesting fundamental time algorithm.
Title: Re: Time algorithms C++ ?
Post by: Nexus on November 25, 2014, 07:16:24 pm
When you're asked to clarify your problem, you should not simply repeat the non-expressive thread title ::)

What is a "time algorithm"? What do you actually want to achieve?
And how is it related to SFML's System module?
Title: Re: Time algorithms C++ ?
Post by: Redee on November 25, 2014, 09:37:54 pm
I try to know time core of SFML in depth.

Also I just wrote some test.
And my average numbers too much different from 168 to 173 in five its tests.

Its simply based on standard time.h library to measure CPU ticks.

Please look at this and give me right decision if my code have some reason.

#include "time.h"
#include "iostream"
#include "random"
#include "vector"
using namespace std;

void main()
{
        clock_t t;
        vector<clock_t> ve;

        for(int n=0; n<100; n++)
        {
                t = clock();
                for(int x=0; x<100000; x++)
                {
                        random_device rd;
                        rd();
                }
                ve.push_back( clock()-t );
        }
       
        int vSz = (int)ve.size();
        int sum=0;
        int z=0;
        for( ; z < vSz; z++)
        {
                sum += ve[z];
        }

        cout << "List of 100 numbers of CPU ticks:" << endl;
        for(z=0; z < vSz; z++)
        {
                cout << ve[z] << endl;
        }
        cout << "---" << endl;
       
        int avgNum = sum / vSz;
        cout << "Average ticks number: " << avgNum << endl;

        cout << "Average time in seconds: "
        << (double)avgNum / CLOCKS_PER_SEC
        << endl;
}
Title: Re: Time algorithms C++ ?
Post by: Ixrec on November 25, 2014, 09:52:53 pm
It's still not clear what you're looking for, though I'm not sure there's much we can do about the massive language barrier here.

In the hopes of answering whatever it was you were trying to ask, here are a few random statements:

- There's nothing hugely "wrong" with that example code, though it's a bit odd to use time.h instead of std::chrono if you have access to C++11 (among many other smaller issues).

- You will never get a program that runs in the exact same amount of time on every machine, every time you run it.  That simply isn't possible, and it never will be.  Instead, you should write code that won't break if something takes a few milliseconds longer than you expected.

- If you don't know how to make games that can cope with this variance in execution speed, read http://gameprogrammingpatterns.com/game-loop.html.  If you're making some other kind of program, it shouldn't be much of a problem at all.

- If you want to know how SFML's time-related classes work, just go read the source code (https://github.com/SFML/SFML/tree/master/src/SFML/System).  If you're any good at C++, SFML's source is very easy to read.

If none of that answered your question, please be far more specific and detailed in your next post, so that we might be able to figure out what you're trying to say.
Title: Re: Time algorithms C++ ?
Post by: Redee on November 25, 2014, 10:27:22 pm
Thank you maybe in more accuracy I will can mesure fast parts of code.
Because clock() only in milliseconds.
And often return 0 at simply fast parts of code.
Now trying with #include <chrono> but no see at VS2010.
Title: Re: Time algorithms C++ ?
Post by: Ixrec on November 25, 2014, 10:30:03 pm
If all you want is a higher timer resolution than milliseconds, then <chrono> is definitely the place to look.
Title: Re: Time algorithms C++ ?
Post by: Redee on November 25, 2014, 10:34:50 pm
Its make life harder...

http://stackoverflow.com/questions/18615439/visual-studio-2010-chrono-header-file-missing

Quote
Chrono is a standard template library that has been introduced in Visual Studio 2012. Chrono is used to manipulate time durations and time instants.
Visual Studio 2010 or even with SP1 will not support Chrono.
Title: Re: Time algorithms C++ ?
Post by: Gambit on November 25, 2014, 10:36:51 pm
Upgrade to 2012 then, whats the problem?
Title: Re: Time algorithms C++ ?
Post by: Redee on November 25, 2014, 10:37:39 pm
Yes its problem because cant install Win7 to my old PC.
Title: Re: Time algorithms C++ ?
Post by: Ixrec on November 25, 2014, 10:40:01 pm
Either update to a less outdated OS and compiler (which you should do anyway), or do some research on the OS-specific API you'll need to implement higher-resolution timing yourself.

Also, if the goal is just to measure how fast your program executes, you may want to look at "profilers", rather than agonizing over how to make your program profile itself in great detail. (I'm still not sure exactly what you're after)
Title: Re: Time algorithms C++ ?
Post by: eXpl0it3r on November 25, 2014, 10:48:53 pm
SFML uses the fastest available timing mechanism on each platform, so there's really no issue using it.
Title: Re: Time algorithms C++ ?
Post by: Redee on November 25, 2014, 10:56:27 pm
Try to know about this at sources.

I now thinking and... think its must be based on constant parallel no changeable mechanic process of machine.
And during comparison we can do something exactly.

And else question is >
which technology is embedded in an electronic clock watch.

Why so difficult at computers to know how it arranged.

Maybe its help
https://en.wikipedia.org/wiki/Time_Stamp_Counter
http://msdn.microsoft.com/en-us/library/twchhe95%28v=vs.100%29.aspx
And logically we must measure 1 CPU step in nanoseconds to know what is equals.
Its interesting.

Then I go to dig sources SFML.

Found key to know at source >> ...\src\SFML\System\Win32\ClockImpl.cpp
Maybe its no pure cross-platform because have also this way >> ...\src\SFML\System\Unix\ClockImpl.cpp
Its dynamically when compile.
Title: AW: Time algorithms C++ ?
Post by: eXpl0it3r on November 26, 2014, 01:16:06 am
If you want to talk about your research, then open a blog and don't post it on the SFML forum. We've done our research already and use the most precise and reliable way on each OS.

As for the rdtsc, read the link in the comments: http://msdn.microsoft.com/en-us/library/windows/desktop/ee417693%28v=vs.85%29.aspx
Title: Re: Time algorithms C++ ?
Post by: Redee on November 26, 2014, 01:13:18 pm
Ok I understand, thank you.

Yes I newbie but there says to minimal use in game!
>>
http://msdn.microsoft.com/en-us/library/windows/desktop/ee417693.aspx
Quote
Nevertheless, developers should attempt to have their games call QueryPerformanceCounter as little as possible to avoid any performance penalty.

Yes now I know need only 1 call, alright, no problem with QPC now )).

timeGetTime() and GetTickCount() as old reliable solution to measure time.
Else this needs link Winmm.lib (if you instal MS Visual Studio can find at C:\Program Files\Microsoft SDKs)
And at windows system32(64) path will be Winmm.dll
For example its using famous old game Quake3.
Title: Re: Time algorithms C++ ?
Post by: Hapax on November 28, 2014, 02:43:24 am
http://msdn.microsoft.com/en-us/library/windows/desktop/ee417693.aspx
Quote
Nevertheless, developers should attempt to have their games call QueryPerformanceCounter as little as possible to avoid any performance penalty.

Quote
can be called several hundred times per frame without any noticeable impact
Title: Re: Time algorithms C++ ?
Post by: Redee on November 29, 2014, 02:55:59 pm
timeGetTime() faster but no need thats amount of calls ))
Simple test >
(click to show/hide)
Title: Re: Time algorithms C++ ?
Post by: Nexus on November 29, 2014, 03:24:53 pm
What's your point? Which time measurement technique you use depends on the needed resolution...
Title: Re: Time algorithms C++ ?
Post by: Redee on November 29, 2014, 07:15:22 pm
Nexus thank you, your advices very helpful.

In main game logic - I have global gameTimeCounter variable which refresh every frame.
All objects compare with this variable.
If more then minimal value after comparison - object do my action.

timeGetTime() is faster.
QPC is much more powerful but have some bugs.

I again here and very sorry - on old windows systems can be bugs with QPC.
http://msdn.microsoft.com/en-us/library/windows/desktop/dn553408.aspx

Quote
Windows XP and Windows 2000

QPC is available on Windows XP and Windows 2000 and works well on most systems. However, some hardware systems' BIOS didn't indicate the hardware CPU characteristics correctly (a non-invariant TSC), and some multi-core or multi-processor systems used processors with TSCs that couldn't be synchronized across cores. Systems with flawed firmware that run these versions of Windows might not provide the same QPC reading on different cores if they used the TSC as the basis for QPC.

Here also explains about possible issues.
http://www.virtualdub.org/blog/pivot/entry.php?id=106
(click to show/hide)

And here a little bit explains about bugs of QPC.
http://stackoverflow.com/questions/14363398/queryperformance-counter-in-multicore-systems-with-variable-clock-speeds

Who can use new vers. of MSVS (from 2012 and higher) - use <chrono> library for time measure.
If develop on old visual studio for old windows versions also - my advice use timeGetTime().