Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Farflame |
New to Godot. On my learning project, I’m loading in a text file. It’s a list of surnames, about 10,000 names long. It’s reading them line-by-line and appending them to an array. It’s very slow, takes about 2 seconds. It’s instantaneous in other languages, this sort of thing isn’t really a problem nowadays.
I’ve tried loading it as a single string and then splitting it, but it still takes a long time. I’ve also tried just loading it and not adding it to an array, thinking that maybe the array was slow, but it’s still the same.
Is this a known issue or am I doing something wrong? My code is just a very simple…
var f = File.new()
f.open("res://Files//firstnames//english.txt", f.READ)
while not f.eof_reached():
Firstnames.append(f.get_line())
f.close()
For now I’ve just reduced it from 10,000 names to 10 and it’s super-fast, so it does appear to be struggling to read the file.
Loading a file that long can take some time. With GDscript, there’s a tiny bit of overhead (part of the downsides with a dynamically typed language).
Do you require 10,000 names? Could you cut it down to 1,000, or even 100?
Ertain | 2018-10-29 23:36
For testing I’ve cut it down to 10 which is instant. I could probably manage with 1,000 eventually but I’d prefer the entire list. In Unity, GM and VS, this happens in the blink of an eye, seems strangely slow here.
Farflame | 2018-10-30 02:49
how about just using f.get_line()
without appending it to the array?
volzhs | 2018-10-30 05:23
A suggestion would be to read the entire file in one go using content=File.get_as_text()
instead of seeking for the next linebreak, and then split the text into an array using Firstnames=split("\n",content)
Depending on os it may be combinations of \n,\r,\n\r you need to split on - ideally you would split on the regEx /[\n\r]+/ but I can see a way to that using gdscript.
For more complex data I would suggest using parse_json
and storing the data as JSON.
If you want to use gdscript to convert the data, do as you do now and then write the result of to_json(Firstnames)
to a file - that should result i a noticeable speedup
usefull links;
@GDScript — Godot Engine (latest) documentation in English
@GDScript — Godot Engine (latest) documentation in English
guppy42 | 2018-10-30 08:46
I did try reading it as one line and then splitting it but it still took a long time. Based on the answer below, I’m thinking maybe that it’s something to do with my file, although it does read fast in other languages. It does at least look like there may be a solution to this, thanks for the replies.
Farflame | 2018-10-30 14:49