How to get current time in milliseconds?

How to get current time in milliseconds?
0
#1

Hi there.
Anyone can tell me how to get current time in milliseconds ?
i was try with TimeUtils::currentTimeMillis() but seems it’s not exactly.
Thanks you.

0 Likes

#2

There are so many ways one can accomplish this: http://www.cplusplus.com/reference/ctime/time/

0 Likes

#3

Thanks you but i need get the time in millisecond. And get in both win32 and android.

0 Likes

#4

long long getCurrentTime()
{
struct cc_timeval tv;
CCTime::gettimeofdayCocos2d(&tv, NULL);
long long time = ((long long)tv.tv_sec) * 1000+ tv.tv_usec / 1000;
return time;
}

0 Likes

#5

where is CCTime class?
I search in cocos2d but only found cocos2d::Timer.

0 Likes

#6

My cocos version is 2.2.2; There is cocos2d::CCTime, and I am not found cocos2d::Timer;

0 Likes

#7

Well, i am using cocos v3.1.
I was found a way from google:

auto timeSinceEpoch = std::chrono::duration_caststd::chrono::nanoseconds(std::chrono::system_clock::now().time_since_epoch()).count();

0 Likes

#8

If you’re not using c++11 you could perhaps create a time class, capture time(0) and clock() at startup and calculate a ‘rough’ fit.

If you are using c++11 use the chrono library :stuck_out_tongue:

http://en.cppreference.com/w/cpp/chrono

How are the values going to be used? If you’re wanting to time something in milliseconds you don’t need ‘the current time’, you just need to know the difference between timeA and timeB. which can be achieved by clock alone.

1 Like

#9

yes, i want to get computation time in game logic loop.
I use folowing code and get time between game logic function:

struct timeval now;
if (gettimeofday(&now, nullptr) != 0)
{
	CCLOG("error in gettimeofday");
	return 0;
}
return (now.tv_sec) + (now.tv_usec) / 1000000.0f;	

But sometimes, the result returned is quite large (0.05s) whether the loop is not calculated everything.
The fps is unstable, sometime its down to 2x.
I must find out the problems so i need to be sure the time calculation is right.

1 Like

#10

I felt like the link I provided gave the info you needed except the milliseconds conversion.

There are a lot of ways to do this. literally I can think of 5

timeval curTime;
gettimeofday(&curTime, NULL);
int milli = curTime.tv_usec / 1000;
time_t _time = time(0);
struct std::tm _now = *localtime(&_time);
struct std::tm _end = <get some ending time>;

double _diff = difftime(mktime(&_end), mktime(&_now));
time_t _time = time(0);
std::chrono::system_clock::time_point _now = std::chrono::system_clock::from_time_t(_time);

can use a std::chrono::duration_cast<std::chrono::milliseconds>(_end - now).count()

0 Likes

#11

If you just want to time a piece of code use clock()

See the example here:

http://www.cplusplus.com/reference/ctime/clock/

Also have you tried profiling? Timing code can be tedious and the profiling tools provided by Xcode can really help to speed up the process by giving you readouts for each function and its CPU use relative to other parts of your code.

https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/Introduction/Introduction.html

0 Likes

#12

I have tried many different ways to time a piece of codes. The results are quite similar.
But I can not understand why the test results are back so unusual.
In my update(float dt) function I get:

time A;
time B1;
log("time before call %f",b1-a);
checkAndRemoveChain();
time B2;
log("time calculation %f",b2-a);

In checkAndRemoveChain() function i also do the same. And log time calculation of this function.
The log show:

time before call 0.001000
time run checkAndremove 0.001000
time calculation 0.053000

I do not understand what’s happened. Sometimes, time before call = 0.05 or time run checkAndremove = 0.05 although it is not compute multiple in which case (only compare the boolean variable)
I have tried with three ways but also almost similar results.

0 Likes

#13

You should move the log(“time before call %f”, b1-a); call out until the other log() function.

Things like logging, which are I/O, can be heavy timing and syncronization factors, so you want to perform them outside of the region you are testing.

Otherwise-- the extra 0.05 seconds that occurs randomly could be v-syncing, or similar sync-with-GPU-rendering op.

And anyway, to the other posters: yes there are a lot of time-measuring functions, but Cocos is cross-platform, and different systems have varying sensitivity. With 64-bits you have over 500 years of addressable nanoseconds. A function like u64 TimeUtils::getCurrentNanos() … that returns the highest precision counter on your OS would be useful!

0 Likes

Get dt in milliseconds
#14

@tieudaotu

The simplest way to get current time in milliseconds is:

auto timeInMillis = std::time(nullptr); //returns long type

Also do remember to include:

#include <time.h>
0 Likes

#15

Since this didn’t seem to be answered and I was looking for the answer myself… the COCOS2D API for this seems to be:

utils::getTimeInMilliseconds();

It lives in ccUtils.h (Cocos 2d-x 3.9)

1 Like

#16

There are so many ways to answer this.

0 Likes

#17

This function results in different values across platforms and can’t be used for things like network synchronisation.

Does this functions results vary over time? If it was called 10 times per second would it result in varying differences?

0 Likes

#19

Not possible to control that behavior for offline game.
If its online game then you can cross check with your backend.

0 Likes

#20

I don’t have a backend, has anyone maybe called a public API to get the current datetime or something similar?

0 Likes

#21

you could call apples, googles, etc - time server
ill guess they will have a decent server uptime

0 Likes