Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Time algorithms C++ ?  (Read 9900 times)

0 Members and 1 Guest are viewing this topic.

Redee

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Time algorithms C++ ?
« 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 ?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Time algorithms C++ ?
« Reply #1 on: November 25, 2014, 06:28:15 pm »
What do you mean? In SFML, there is sf::Clock, which works portably.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Redee

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Re: Time algorithms C++ ?
« Reply #2 on: November 25, 2014, 06:47:37 pm »
Me interesting fundamental time algorithm.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Time algorithms C++ ?
« Reply #3 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?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Redee

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Re: Time algorithms C++ ?
« Reply #4 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;
}
« Last Edit: November 25, 2014, 10:11:56 pm by Redee »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Time algorithms C++ ?
« Reply #5 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.  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.
« Last Edit: November 25, 2014, 09:55:13 pm by Ixrec »

Redee

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Re: Time algorithms C++ ?
« Reply #6 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.
« Last Edit: November 25, 2014, 10:32:10 pm by Redee »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Time algorithms C++ ?
« Reply #7 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.

Redee

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Re: Time algorithms C++ ?
« Reply #8 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.

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Time algorithms C++ ?
« Reply #9 on: November 25, 2014, 10:36:51 pm »
Upgrade to 2012 then, whats the problem?

Redee

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Re: Time algorithms C++ ?
« Reply #10 on: November 25, 2014, 10:37:39 pm »
Yes its problem because cant install Win7 to my old PC.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Time algorithms C++ ?
« Reply #11 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)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10838
    • View Profile
    • development blog
    • Email
Re: Time algorithms C++ ?
« Reply #12 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Redee

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Re: Time algorithms C++ ?
« Reply #13 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.
« Last Edit: November 26, 2014, 12:22:45 am by Redee »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10838
    • View Profile
    • development blog
    • Email
AW: Time algorithms C++ ?
« Reply #14 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
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything