This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Monkeymad358's profile picture

How many commands can a computer perform a second?

Started by Monkeymad358, 15 December 2014 - 09:29 PM
Monkeymad358 #1
Posted 15 December 2014 - 10:29 PM
This is more of a general question rather than a problem, as I was wondering how many commands a computer can perform per tick. Is the limit how fast my computer is or is it an in-game limit, and if so then what is it? I'm assuming there may be a way of working it out by using for loops, by looping a command as many times as possible for a certain amount of time, and them working out how many times the program looped. I can not currently test this out as I am not on my computer, but I will definitely try it when I can.
Lyqyd #2
Posted 15 December 2014 - 11:42 PM
Moved to Ask a Pro.

There's not really a unit known as "commands", and giving an instructions per second figure wouldn't be useful since you'd have to know about the specifics of Lua bytecode and how the Java side of things plays in to all of this. Generally, though, the speed of execution of ComputerCraft programs depends on a number of factors:

The processing speed of the server.
The tick rate of the server.
How many computers are actively wanting to run.

All of the computers on a server (local or remote, so SSP and SMP) run in a single thread, and take turns. Only one computer is ever actually executing code at a time. The rest are all yielded, waiting their turns. The more computers waiting, the longer it can take for it to be a given computer's turn again. The server tick rate seems to be similar in principle, the more work the server overall is trying to do, the slower the computers are. The last factor is server processing speed. If the other two factors aren't slowing things down, the computers will execute as fast as the server's processor can do so.

On SMP, network latency can also play a role in perceived slowness, since inputs must travel to the server, be computed, and any screen changes be sent back to the client. This does not affect the actual speed of the computers.
Monkeymad358 #3
Posted 16 December 2014 - 10:29 PM
Ah I see, that makes a lot of sense now. +1 for a very interesting post :)/>
theoriginalbit #4
Posted 16 December 2014 - 11:58 PM
To extend upon what Lyqyd stated, the computers all take turns in running. So at any given point in time only one computer is running.

As such there is a failsafe built into ComputerCraft where if a computer is taking up too much time, and not yielding control so the next computer may run, ComputerCraft will terminate the script — with a 'too long without yielding' error — so that the next computer may run. The time this takes can vary depending on a few factors, as Lyqyd stated, but it is generally around the 5 to 10 second mark. There are several different functions which all yield, mostly if they have to wait for some form of response; sleep, read, rednet.recieve, all turtle functions, all http functions, just to name a few, as well as the obvious os.pullEvent, os.pullEventRaw, and coroutine.yield which are the three functions that you can use to yield and wait for an event.

It has been known, and I've personally seen it, that on the rare server the tick rate and CPU power is so bad that most scripts can't run even a single instruction, with each computer being error'd out when it can't possibly yield. Though as I said, this is rare, it requires a server that simply isn't designed to be running Minecraft, especially when there gets to be too many ticking TileEntities (machines, computers, etc), which just forces ComputerCraft to a grinding halt.

EDIT: As Lyqyd also stated giving an instructions per second figure isn't useful, especially since all computers run differently and have different capabilities and the likes. However just to show the variation, and how large a number it is, I threw together a little script that simply loops for as close to a second as possible. The code is as follows:

local i = 1
local s = os.clock()
repeat
  i = i + 1
until (os.clock() - s) > 1
print(os.clock() - s)
print(i)
and it yielded the following results across five tests — each test took 1.05 seconds to complete

529779
1407596
1344768
1403045
1262787
as you can see there is a large variation in how much it can run, and it all depends on the CPU load. It should also be noted that the above numbers are simply the i value, the loop itself is several instructions in itself. I'm not too sure on the exacts, but I am guesstimating that the loop is approximately 9 instructions long + however many instructions are internally within os.clock.
Edited on 16 December 2014 - 11:17 PM