Niel Drummond wrote:
Hello,
I am looking at how neko handles simple file io situations, so that
one may bind it to useful C functions like sort and join for data
analysis purposes, but I cannot get reading from stdin fast enough. If
I create a long file 100000 lines or more and parse it line-by-line,
it is quite slow:
<snip>
Going through the file took 0.8 seconds, whereas perl took 0.06 seconds
Is there a better way of reading from stdin ?
- Niel
There are two things in your code that probably contribute to it being
slow: using recursion and creating a new string and copying the old one
each time you add a character.
Here is a way to do it with a buffer and a loop instead:
var stdin = $loader.loadprim("s...@file_stdin", 0);
var read_char = $loader.loadprim("s...@file_read_char", 1);
var buffer_new = $loader.loadprim("s...@buffer_new", 0);
var buffer_add_char = $loader.loadprim("s...@buffer_add_char", 2);
var buffer_string = $loader.loadprim("s...@buffer_string", 1);
readline = function() {
var break_char = 10;
var b = buffer_new();
var in = stdin();
try {
do {
var c = read_char(in);
if(c == break_char)
break;
buffer_add_char(b,c);
} while(true);
}
catch e {
return null;
}
buffer_string(b);
}
while ((line = readline()) != null) {}
In my testing, this was about 80% faster.
Hope that helps.
-Justin
--
Neko : One VM to run them all
(http://nekovm.org)