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

Cycles calculator for your computer craft gadgets.

Started by trey777, 01 April 2016 - 09:27 PM
trey777 #1
Posted 01 April 2016 - 11:27 PM
Hello, everyone. I have a new API for you guys to try out.

It calculates the cycles that are running on your script. So you know how many times per second the computer reads at.

in my current project which is making a decent networking system i use it just to see the likely hood of dropping a packet.

the max speed is 20 C/ps. or sleep(.01) in your main loop with nothing else in the way.

Feel free to use this little gadget any time and if youd like to give credit you can for the idea or scource code im your guy ;)/>. i know most of you are going to fuck with this thing and make it for your own needs so have at it.


version 1.0 - its not really an api. just a cool function used for data collection.



get_speed_table = {	 -- create table for data
speed_conversion = "cp/s",	-- cp/s cp/m
selected_speed_units = 1,   -- change for 1 = frames/1  60 = frames/60
speed_conversion_units = {1,60}, -- unit table
first_run = true,	 -- start up with the right units
cycles = 0,	   -- the number of revolutions total
speed = 0,	   -- out put after conversion
origin_time = os.clock(),   -- set origional time
time_forward = origin_time,   -- add 1 second to the origion time
time = 0,
}


function GET_SPEED()
if get_speed_table.first_run == true then
if get_speed_table.speed_conversion == "cp/s" then
  get_speed_table.time_forward = get_speed_table.origin_time +1
  get_speed_table.first_run = false
else
  get_speed_table.time_forward = get_speed_table.origin_time +60
  get_speed_table.first_run = false
end
end
if get_speed_table.first_run == false then
  get_speed_table.cycles = get_speed_table.cycles+1
  get_speed_table.time = os.clock()
  if get_speed_table.time >= get_speed_table.time_forward then
   if get_speed_table.speed_conversion == "cp/s" then
	get_speed_table.speed = get_speed_table.cycles/get_speed_table.speed_conversion_units[get_speed_table.selected_speed_units]
	get_speed_table.cycles = 0
	get_speed_table.time_forward = get_speed_table.time +1
   end
   if get_speed_table.speed_conversion == "cp/m" then
	get_speed_table.speed = get_speed_table.cycles/get_speed_table.speed_conversion_units[get_speed_table.selected_speed_units]
	get_speed_table.cycles = 0
	get_speed_table.time_forward = get_speed_table.time +60
   end
  end
end
return get_speed_table.speed, get_speed_table.cycles, get_speed_table.speed_conversion
end -- eof

while true do
s,c,sc = GET_SPEED()
print("speed: "..s.." in: "..sc.."cycles counter: "..c)

sleep(.01)
term.clear()
term.setCursorPos(1,1)
end



here is a picture for validation:

my settings are the same as in the test code above.


ALSO: if you are interested in swapping settings on the fly this wont do it. just runs it how it is configured


Edited on 01 April 2016 - 09:48 PM
valithor #2
Posted 01 April 2016 - 11:50 PM
Just going to point out you can go way over 20 cp/s

Instead of using:

sleep(.01)

Use:

os.queueEvent("event")
coroutine.yield()

Got it to cap out at 1700 cp/s in an emulator. Fun little program good work.
trey777 #3
Posted 01 April 2016 - 11:58 PM
holly shit dude 0.0 its like in the 3,000's check it

what makes this work like that??


and thanks man i didn't work to hard on it or nothing ^^ i just figured it could help some kids out around the net.


Edited on 01 April 2016 - 09:59 PM
trey777 #4
Posted 02 April 2016 - 12:09 AM
Just going to point out you can go way over 20 cp/s

Instead of using:

sleep(.01)

Use:

os.queueEvent("event")
coroutine.yield()

Got it to cap out at 1700 cp/s in an emulator. Fun little program good work.



so i have a receiving unit in my script rn and it


sender_id,data,dist = rednet..receive(.01)

and brings it back back down to 20cp/s

im wondering is there a way to use corutine to make this faster?? im not totally sure on how to use the api at all.

you seem to be an enlightened on the subject, wanna help a dude out rq??
Edited on 01 April 2016 - 10:10 PM
valithor #5
Posted 02 April 2016 - 12:46 AM
The reason sleep slows it down is due to what it is doing internally. What it does is start a timer, and the minimum amount of time a timer can wait before firing is .05 seconds, hence the 20 cp/s.

All what I did was doing was queueing an event, and then pulling the event. The reason it is faster is it does not have to wait for a timer to fire, thus the only limit is how fast your computer can run the code. It is a good way to prevent your script from erroring with the too long without yielding, but it is not possible to wait a specific amount of time with it.
Edited on 01 April 2016 - 10:46 PM
trey777 #6
Posted 02 April 2016 - 12:56 AM
is there any way to speed up a rednet.receive() or a modem. os.pullEvent("modem_message") or is this the fastest know way currently to send and receve a file…

the faster i can get the programs to think the less likely my apis will loose a packet. since its a TCP netowk rn only packet loss is compencated for by re sending.

most of my code is based on timers…

like is there a way to shorten the receiving lengeth… rn rednet.receive(1) is to slow and bulky for this system. is there a way to shorten the firing length or rec functions shorter than .05
Edited on 01 April 2016 - 10:59 PM
valithor #7
Posted 02 April 2016 - 01:06 AM
Keep in mind that whenever a computer is running code it doesn't necessarily miss "packets". For CC computers there is a event queue, where if any event is fired it is put into that queue. So if the computer is running and it receives a rednet message, then it is just added to the event queue until you check it. However, there is then the problem of functions that "eat" events, such as sleep. By using os.pullEvent("modem_message") that essentially says listen only for modem_messages, and discard any other event until one is found. Because sleep is listening only for timer events, it will discard any other event until the timer event is found.

With all of that in mind it is really just planning and structuring your code so events are not lost. It might also include rewriting or not using certain apis. It is perfectly fine to use timers, but sleep is usually something that is not good to use for systems that depend heavily on events.

I feel like we are straying from the topic of your program. I am willing to help just probably better to do it in pm. :P/>
trey777 #8
Posted 02 April 2016 - 01:53 AM
i pm'd you^^