Posts: 371
Threads: 66
Joined: Oct 2012
Reputation:
0
16.12.2012, 13:36
(
Последний раз редактировалось dr.lozer; 16.12.2012 в 17:45.
)
i need a function that convert seconds into other style
EXAMPLE:
61 seconds into 1:1
EDIT: nvm i found it :/
Posts: 734
Threads: 8
Joined: Jun 2009
I wouldn't go with the "while" loop. It's uber slow. Don't know if it's a side-efect of using a for-loop with while. But I benchmarked your code and my own simplest version without loops and results were shocking.
Код:
[20:22:13] Time taken to execute My SecToMS: 1066ms
[20:22:45] Time taken to execute LarzI's SecToMS: 31218ms
The code:
pawn Код:
#define FILTERSCRIPT
#include <a_samp>
public OnFilterScriptInit() {
new f_Tick, l_Tick;
f_Tick = GetTickCount();
for (new d; d < 1000000; d++)
SecondsToMinutesSeconds(54684);
l_Tick = GetTickCount() - f_Tick;
printf("Time taken to execute my SecToMS: %dms", l_Tick);
f_Tick=0; l_Tick=0;
f_Tick = GetTickCount();
for (new d; d < 1000000; d++)
ConvertSecondsToMinutes(54684);
l_Tick = GetTickCount() - f_Tick;
printf("Time taken to execute LarzI's SecToMS: %dms", l_Tick);
f_Tick=0; l_Tick=0;
return 1;
}
stock SecondsToMinutesSeconds(seconds)
{
new mins, sec;
mins = seconds/60; // Divide by 60 to get minutes
sec = seconds%60; // Modulo 60 gets the remainder of a division, meaning the left-over seconds.
new string[12];
format(string, 12, "%d:%02d", mins, sec);
return string;
}
stock ConvertSecondsToMinutes(seconds)
{
new
minutes,
output[ 8 ]; //8 cells. This allows the maximum output to be XXXX:XX - I don't think you will exceed 9999 minutes and 59 seconds.
while( seconds > 60 )
{
minutes ++;
seconds -= 60;
}
format( output, sizeof( output ), "%02i:%02i", minutes, seconds );
return output;
}
Posts: 686
Threads: 29
Joined: Feb 2011
Reputation:
0
Haha! Wanted to use modulo too but for some reason my brain stopped working, thank you Virtual1ty! You just refreshed it.