Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
MeltingBrain

Optimization for Read

Question

Hello, I would like help optimizing my custom filesystem library, more precisely the 'readLine' function.
It takes ages to read one line, and I think it's because it's badly optimized.

 

Here is the code:
 

local line = ""
local data = nil
repeat
local data = fs.read(handle, 1) -- Reads 1 character
if data and data ~= "\n" then
line = line .. (data or "")
else
data = nil
end
until not data
return line ~= "" and line or nil

If possible, it would be nice to help me upgrade the code, as at the end you see it should return nil instead of a text, it's my bad code and in the final return text, there is sometimes a weird icon at the end (the \n char when printing the result).

Thanks from advance.

 

Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Part of the reason you can't detect newline characters is because your data variable will only ever be one char where the newline pattern is a 2 char string. As for the rest I don't see the point of reading files with a custom lua func. The java Scala background can do this better than the ingame lua interpreter.. Reason?

Link to post
Share on other sites
  • 0

Part of the reason you can't detect newline characters is because your data variable will only ever be one char where the newline pattern is a 2 char string. As for the rest I don't see the point of reading files with a custom lua func. The java Scala background can do this better than the ingame lua interpreter.. Reason?

 

I can detect a new line, as \n is one byte (Special Character for end of line), and it reads 1 byte per scan.

 

But OpenComputers made it VERY SLOW to read 1 per 1 until new line...

 

I'll search more about the java Scala as you said.

 

+ TO THE MODS CHECKING MY POST: Please say thanks from me to fnucke.

Link to post
Share on other sites
  • 0

Yes, reading a file byte by byte is slow. This is normal. If you were to read a file byte by byte that'd also be - comparatively - very very slow.

 

That's why there's buffering, and when you open a file using io.open you get a buffered handle and can do this way more comfortably, too (`f=io.open('blah'); f:read(1)`).

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.