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

Could you please help optimize this code?

Started by Nhorr, 14 July 2015 - 06:14 PM
Nhorr #1
Posted 14 July 2015 - 08:14 PM
TL;DR: Made a really simple UI, and I know there's got to be a way to shorten at least the password startup section. Help/suggestions appreciated.

NhUI v0.1 pastebin: http://pastebin.com/pcE56R9X

Hey, I don't usually try messing around with UIs, but now that I have, I know there's got to be a way to group a few things here and there to shorten the code. It's a really simple project I'm calling "NhUI" mostly for personal use, and it's current version (first version, really) barely has any functions - it's just going to serve as a platform for later versions I'll be working on.

Really what I'd like to focus on is shortening the code of the password startup text.
That, and making a table for the Desktop buttons rather than having them coded as they are now.
Any suggestions, rewrites, or just general thoughts would be appreciated.

Like I said, it's mostly for personal use, but I'll gladly give you credit in the code.

Thanks in advance!
-Philip (Nhorr)
Edited on 14 July 2015 - 06:23 PM
Lupus590 #2
Posted 14 July 2015 - 09:54 PM
What are you trying to optimise? The speed of the code execution, the size of the code file, or the readability of the code?
Grim Reaper #3
Posted 14 July 2015 - 10:00 PM
In areas that you redraw to add dots to signify loading, try this.


term.clear()
term.setCursorPos(1, 1)
term.write("Loading secure startup")
textutils.slowPrint("...", 3.75) -- 3 chars in .8 seconds -> 3 / .8 = 3.75 chars/sec
-----------------------------------
-- Don't need to clear the screen; print jumps to the next line.
print("No errors detected.")
sleep(0.3)
-----------------------------------
term.write("Starting password protocols")
textutils.slowPrint("...", 3.333) -- 3 chars in .9 seconds -> 3.333 chars/sec
-----------------------------------
term.clear()
term.setCursorPos(1, 1)
sleep(0.4) -- Don't know why this is desired (black screen for almost half a second).
-----------------------------------
print("Password required\nPlease type in the password.")

Or, if you like, you can make a function out of it.

local function loadingPrompt(prompt, numPeriods, totalTime)
    write(prompt) -- Handles \n characters but doesn't append one to the end.
    textutils.slowPrint(string.rep(".", numPeriods), numPeriods / totalTime)
end

term.clear()
term.setCursorPos(1, 1)
loadingPrompt("Loading secure startup", 3, 0.8)

print("No errors detected.")
sleep(0.3)

loadingPrompt("Starting password protocols", 3, 0.9)

term.clear()
term.setCursorPos(1, 1)
sleep(0.4)

print("Password required\nPlease type in the password.")
flaghacker #4
Posted 15 July 2015 - 08:44 AM
I would remove the useless and fake wait times and prints. Users don't like them. Your code will be a lot cleaner and it will run a lot faster.


print ("junk")
sleeo (1)
print ("no errors detected")
Off course no errors detected, it was just a sleep! Why do you have them there?
Nhorr #5
Posted 15 July 2015 - 02:01 PM
Alright, got what I mainly needed.
Thanks Grim Reaper, and noted flaghacker (like I said, mostly for personal use, but I'll probably limit it to like 2 small wait times if not less).
H4X0RZ #6
Posted 15 July 2015 - 03:18 PM
If you don't care about readabilty: make it as big as you want, add comments, unnecessary spaces etc. When you are ready to upload it to pastebin (or whatever you use), use something like LuaSrcDiet to minimize the code (mostly it removes comments, spaces which are not needed and unnecessary linebreaks) which really decreases the filesize.
For example my binary API: normal and compressed. The size is roughly 1/2 of the original at the cost of readability.

Else you should do what Grim and flaghacker told you. Fake loading just slows your program down (a LOT) and you don't have to clear the screen every time. If you don't want to use the textutils.slowPrint method (which is better in this situation) you could also do something like this: (this should be faster when you want it to be something like an animation.)


local cycles = {"   ",".  ",".. ","..."," ..","  .","   "}
local message = "Starting"

for i = 0,math.huge do
  term.setCursorPos(1,1)
  print(message..cycles[(i%#cycles)+1])
  sleep(.1)
end
Edited on 15 July 2015 - 02:59 PM
Nhorr #7
Posted 15 July 2015 - 04:53 PM
Thanks for the tip, H4X0RZ!
Nhorr #8
Posted 15 July 2015 - 05:42 PM
Using the info I've gotten back, this is v0.1.1a:
http://pastebin.com/QsuivcDR
Still working on it, but cut a lot of the fake loading sequences out, for a start.
I also added your guys' suggestions for optimization.


local cycles = {"   ",".  ",".. ","..."," ..","  .","   "}
local message = "Starting"

for i = 0,math.huge do
  term.setCursorPos(1,1)
  print(message..cycles[(i%#cycles)+1])
  sleep(.1)
end
H4X0RZ, the code just causes a constant animation loop. Trying to solve this myself, but wouldn't mind the extra hand.

EDIT: (Nevermind, found a fix.)

local cycles = {"   ",".  ",".. ","..."," ..","  .","   "}
local message = "Starting"

for i=0,20 do --still dialing in max val
  term.setCursorPos(1,5)
  print(message..cycles[(i%#cycles)+1])
  sleep(.1)
end

But yeah, thanks again for all of your guys' help!
Edited on 15 July 2015 - 04:38 PM
Nhorr #9
Posted 16 July 2015 - 03:39 AM
Aaaand finished NhUI v0.2a.

CraftOS line:

pastebin get cEgrT1Qs install

Pastebin link:
http://pastebin.com/cEgrT1Qs

Cheers!
Edited on 16 July 2015 - 01:39 AM