os.clock() vs os.time()

Talk about anything in Runes of Magic. This does not need to pertain to botting.
Post Reply
Message
Author
User avatar
BlubBlab
Posts: 948
Joined: Fri Nov 30, 2012 11:33 pm
Location: My little Pony cafe

os.clock() vs os.time()

#1 Post by BlubBlab » Wed Sep 25, 2013 12:50 pm

I read the lua api after I felt it passed too much time in a case, first both return "seconds" if no argument given to the functions.

From Lua Refrenz:
os.clock ()

Returns an approximation of the amount in seconds of CPU time used by the program.
For those who not know "CPU time" is the the amount of time which has the process scheduler given the process to run on the CPU.

It has very little to do which time has passed

os.time()
Returns the current time when called without arguments, or a time representing the date and time specified by the given table. This table must have fields year, month, and day, and may have fields hour (default is 12), min (default is 0), sec (default is 0), and isdst (default is nil). For a description of these fields, see the os.date function.

So my question is what os.clock () do in rom bot intended?(For coco and the measure of the cpu frequent it make sense )
Jack-of-all-trades, but master-of-only of a few :D

My Reps:
https://github.com/BlubBlab/Micromacro-with-OpenCV (My version of MM2 with OpenCV and for MS Visual Studio)
https://github.com/BlubBlab/rom-bot (rombot with no stop WP and advanced human emulation mode and some other extensions)
https://github.com/BlubBlab/Micromacro-2-Bot-Framework ( A work in progress )
My Tools : viewtopic.php?f=10&t=6226

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: os.clock() vs os.time()

#2 Post by Administrator » Tue Oct 01, 2013 8:17 pm

You're right that os.clock() probably shouldn't be used in the bot scripts. LuaCoco is deprecated in the latest version of MicroMacro as well as using os.clock() to estimate CPU clock speed (this can be pulled from the registry).

I would consider it a bug, but I'm not going to make any changes until rock5 gets some say into it as it likely will cause some headaches for him.

For his (and anyone who is interested) reference, here's every mention of os.clock() in RoM bot:

Code: Select all

D:\sync\micromacro\scripts\rom\classes\bank.lua (1 hit)
	Line 53: 			if (os.clock() - item.LastMovedTime) > ITEM_REUSE_DELAY then
  D:\sync\micromacro\scripts\rom\classes\guildbank.lua (1 hit)
	Line 122: 			if (os.clock() - item.LastMovedTime) > ITEM_REUSE_DELAY then
  D:\sync\micromacro\scripts\rom\classes\inventory.lua (1 hit)
	Line 203: 			if (os.clock() - item.LastMovedTime) > ITEM_REUSE_DELAY then
  D:\sync\micromacro\scripts\rom\classes\item.lua (6 hits)
	Line 510: 		local starttime = os.clock()
	Line 511: 		repeat yrest(50) self:update() until (not self.InUse) or (os.clock() - starttime) > ITEM_REUSE_DELAY
	Line 513: 	elseif (os.clock() - self.LastMovedTime) < ITEM_REUSE_DELAY then
	Line 515: 		repeat yrest(50) self:update() until self.InUse or (os.clock() - self.LastMovedTime) > ITEM_REUSE_DELAY
	Line 519: 			repeat yrest(50) self:update() until (not self.InUse) or (os.clock() - self.LastMovedTime) > ITEM_REUSE_DELAY
	Line 548: 		self.LastMovedTime = os.clock()
  D:\sync\micromacro\scripts\rom\classes\itemtypes.lua (2 hits)
	Line 37: 	--local starttime = os.clock()
	Line 93: 	--print("time",os.clock() - starttime)
  D:\sync\micromacro\scripts\rom\classes\pawn.lua (1 hit)
	Line 1109: 			   os.clock()-v.Time > 10 then
  D:\sync\micromacro\scripts\rom\classes\player.lua (10 hits)
	Line 695: 		local starttime = os.clock()
	Line 706: 		until (os.clock() - starttime) > _duration/1000
	Line 1928: 			local starttime = os.clock()
	Line 1930: 			while self.Stance ~= 0 and 2 > (os.clock() - starttime) do
	Line 1937: 		local lootStart = os.clock()
	Line 1938: 		while target:exists() and (not target.Lootable) and os.clock() - lootStart < .2 do
	Line 2604: 		loopstart = os.clock()
	Line 2749: 		local pausetime = loopduration - (os.clock() - loopstart) -- minus the time already elapsed.
	Line 3946: 		table.insert(self.MobIgnoreList,{Address=target.Address,Time=os.clock()})
	Line 3948: 		table.insert(self.MobIgnoreList,{Address=target,Time=os.clock()})
  D:\sync\micromacro\scripts\rom\Release\rom\classes\bank.lua (1 hit)
	Line 53: 			if (os.clock() - item.LastMovedTime) > ITEM_REUSE_DELAY then
  D:\sync\micromacro\scripts\rom\Release\rom\classes\guildbank.lua (1 hit)
	Line 122: 			if (os.clock() - item.LastMovedTime) > ITEM_REUSE_DELAY then
  D:\sync\micromacro\scripts\rom\Release\rom\classes\inventory.lua (1 hit)
	Line 203: 			if (os.clock() - item.LastMovedTime) > ITEM_REUSE_DELAY then
  D:\sync\micromacro\scripts\rom\Release\rom\classes\item.lua (6 hits)
	Line 510: 		local starttime = os.clock()
	Line 511: 		repeat yrest(50) self:update() until (not self.InUse) or (os.clock() - starttime) > ITEM_REUSE_DELAY
	Line 513: 	elseif (os.clock() - self.LastMovedTime) < ITEM_REUSE_DELAY then
	Line 515: 		repeat yrest(50) self:update() until self.InUse or (os.clock() - self.LastMovedTime) > ITEM_REUSE_DELAY
	Line 519: 			repeat yrest(50) self:update() until (not self.InUse) or (os.clock() - self.LastMovedTime) > ITEM_REUSE_DELAY
	Line 548: 		self.LastMovedTime = os.clock()
  D:\sync\micromacro\scripts\rom\Release\rom\classes\itemtypes.lua (2 hits)
	Line 37: 	--local starttime = os.clock()
	Line 93: 	--print("time",os.clock() - starttime)
  D:\sync\micromacro\scripts\rom\Release\rom\classes\pawn.lua (1 hit)
	Line 1109: 			   os.clock()-v.Time > 10 then
  D:\sync\micromacro\scripts\rom\Release\rom\classes\player.lua (10 hits)
	Line 695: 		local starttime = os.clock()
	Line 706: 		until (os.clock() - starttime) > _duration/1000
	Line 1928: 			local starttime = os.clock()
	Line 1930: 			while self.Stance ~= 0 and 2 > (os.clock() - starttime) do
	Line 1937: 		local lootStart = os.clock()
	Line 1938: 		while target:exists() and (not target.Lootable) and os.clock() - lootStart < .2 do
	Line 2604: 		loopstart = os.clock()
	Line 2749: 		local pausetime = loopduration - (os.clock() - loopstart) -- minus the time already elapsed.
	Line 3946: 		table.insert(self.MobIgnoreList,{Address=target.Address,Time=os.clock()})
	Line 3948: 		table.insert(self.MobIgnoreList,{Address=target,Time=os.clock()})


User avatar
BlubBlab
Posts: 948
Joined: Fri Nov 30, 2012 11:33 pm
Location: My little Pony cafe

Re: os.clock() vs os.time()

#3 Post by BlubBlab » Tue Oct 01, 2013 11:31 pm

The bad thing about os.clock is you wait sometimes longer and sometimes shorter(but always longer than the real time) than can result in unexpected results.
If you change that it can be that you always wait to short in some cases.
Jack-of-all-trades, but master-of-only of a few :D

My Reps:
https://github.com/BlubBlab/Micromacro-with-OpenCV (My version of MM2 with OpenCV and for MS Visual Studio)
https://github.com/BlubBlab/rom-bot (rombot with no stop WP and advanced human emulation mode and some other extensions)
https://github.com/BlubBlab/Micromacro-2-Bot-Framework ( A work in progress )
My Tools : viewtopic.php?f=10&t=6226

User avatar
rock5
Posts: 12173
Joined: Tue Jan 05, 2010 3:30 am
Location: Australia

Re: os.clock() vs os.time()

#4 Post by rock5 » Wed Oct 02, 2013 12:01 am

The reason I always used os.clock() is because os.time() only returns whole seconds. I guess we should be using getTime() and deltaTime() but I always considered that less easy to use (although there is probably nothing wrong with it, I probably just need to get used to it) and it's way more accurate than is usually needed for most applications.

So what are you saying, os.clock() is not supposed to return elapsed time like it does but the cpu time used instead?

The funny thing is the game os.clock() also seems to return elapsed time but the game is in reverse to MM. os.clock() returns whole seconds and os.time() includes fractions of a second. If MMs os.time() also included fractions of a second I would just change all the os.clock()s to os.time()s.

Blubblab, I don't know what you mean. I find os.clock() to be accurate to within a couple of ms even after 30s.

Code: Select all

Command> oc=os.clock() gt=getTime() yrest(30000) oc=os.clock()-oc gt=deltaTime(getTime(),gt) print(oc,gt/1000)
30.003  30.003405969322
  • Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
  • I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
  • How to: copy and paste in micromacro
    ________________________
    Quote:
    • “They say hard work never hurt anybody, but I figure, why take the chance.”
          • Ronald Reagan

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: os.clock() vs os.time()

#5 Post by Administrator » Wed Oct 02, 2013 12:32 am

The funny thing is the game os.clock() also seems to return elapsed time but the game is in reverse to MM. os.clock() returns whole seconds and os.time() includes fractions of a second. If MMs os.time() also included fractions of a second I would just change all the os.clock()s to os.time()s.
Didn't you have to use some addon to get those functions back?

The way it is in MicroMacro is the "correct" way (it is unaltered from Lua). It's weird that those would be switched around in the game.

User avatar
BlubBlab
Posts: 948
Joined: Fri Nov 30, 2012 11:33 pm
Location: My little Pony cafe

Re: os.clock() vs os.time()

#6 Post by BlubBlab » Wed Oct 02, 2013 12:47 am

That is because MM has a high process priority and so long nothing other run with an even or higher priority it is very close to it . I only say what is in the manual and that says it return the CPU times used by the the program.

Generally lua hasn't much time functions, if you measure less than a second you have not much choice than depend on os.clock().
But if you measure big time scales you will lose more and more precision.

AH something I forgot about precession :http://en.wikipedia.org/wiki/IEEE_floating_point
Jack-of-all-trades, but master-of-only of a few :D

My Reps:
https://github.com/BlubBlab/Micromacro-with-OpenCV (My version of MM2 with OpenCV and for MS Visual Studio)
https://github.com/BlubBlab/rom-bot (rombot with no stop WP and advanced human emulation mode and some other extensions)
https://github.com/BlubBlab/Micromacro-2-Bot-Framework ( A work in progress )
My Tools : viewtopic.php?f=10&t=6226

User avatar
rock5
Posts: 12173
Joined: Tue Jan 05, 2010 3:30 am
Location: Australia

Re: os.clock() vs os.time()

#7 Post by rock5 » Wed Oct 02, 2013 2:47 am

Administrator wrote:Didn't you have to use some addon to get those functions back?
Yeah. I have no idea if they reproduce the original behavior of the os functions or not.
Administrator wrote:The way it is in MicroMacro is the "correct" way (it is unaltered from Lua). It's weird that those would be switched around in the game.
You do know I only mean about the fractions, right? The values returned are still the same; os.clock() returns the time since process start and os.time() returns the so called unix or posix time.

I've been doing a bit of reading. It looks like os.time is mainly used to get the actual current time and os.clock is used for timing things and is supposed to represent the time elapsed since the process started. Seeing as os.clock is used to time things then it makes sense that it would be more accurate and from what I've read I think os.time is supposed to return whole numbers only. Do it is definitely the client (or the addon) that has it wrong.
  • Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
  • I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
  • How to: copy and paste in micromacro
    ________________________
    Quote:
    • “They say hard work never hurt anybody, but I figure, why take the chance.”
          • Ronald Reagan

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests