1. I/O: slow or fast
I saw the program on i/o posted before and I changed the read routine
including a preassembled sequence in memory, just to test. See the
difference!
constant
NONE = -1 -- one of three booleans
global function read_textfile (sequence fname)
sequence s
integer char, fh
fh = open (fname, "r")
if fh = NONE then
return ""
end if
s = ""
char = getc (fh)
while char != NONE do
s = append(s, char)
end while
return s
end function
global function read_text_preseq(sequence fname)
object s
atom char, fh, c
c=1
fh = open (fname, "r")
if fh = NONE then
return ""
end if
s =repeat(0,200000)
char = getc (fh)
while char != NONE do
s[c] = char
char = getc (fh)
c=c+1
end while
return s
end function
object ya
atom t
t=time()
?t
ya=read_text_preseq("win32lib.ew")
?time()-t
t=time()
ya=read_textfile("win32lib.ew")
?time()-t
--?ya
2. Re: I/O: slow or fast
Here's a good test of the I/O performance of
Euphoria vs. compiled, optimized C.
I wrote a Euphoria program and a C program to
do the same thing: copy a file.
I made two versions of the Euphoria program, and two
versions of the C program.
The first version copies one byte at a time. The second
copies one line at a time.
Note: The line-at-a-time C program below will *only* work
on files that do not contain 0, and that have a "reasonable"
maximum line length (1000 chars).
On my Pentium-150, using WATCOM C with full optimization,
the char at-a-time version was 2x faster in C. I tested with a
1.2Mb binary file. The line at-a-time version was essentially
the *same* speed, to within the 0.05 second resolution of the
clock. I tested with win32lib.ew since the C program failed to
copy the 1.2Mb binary file (it contained 0's).
Conclusion: On I/O, the gap between Euphoria and C will be
*narrower* than for other operations.
Ralf points out that people frequently append() all the
characters from a file into a sequence, and the appending
is what slows things down. Why doesn't this happen with
other languages, such as C? Because C users don't have the
*luxury* of an append built-in function. They would have to
set up a messy system of mallocing and freeing. So they avoid
doing this, and settle for maximum line lengths, maximum
number of lines etc.
procedure file_copy(sequence source, sequence dest)
-- copy a file from source to dest using Euphoria
integer s, d
object c
s = open(source, "rb")
if s = -1 then
return
end if
d = open(dest, "wb")
if d = -1 then
close(s)
return
end if
while 1 do
c = gets(s)
-- c = getc(s) -- char at-a-time
if atom(c) then
-- if c = -1 then -- char at-a-time
exit
end if
puts(d, c)
end while
close(s)
close(d)
end procedure
atom t
t = time()
file_copy("0.dat", "1.dat")
?time()-t
-------------------------------
#include <stdio.h>
#include <time.h>
char line[1000]; // assume line is never more than 1000 chars.
void file_copy(char *source, char *dest)
// copy a file from source to dest using C
{
int c;
FILE *s;
FILE *d;
s = fopen(source, "rb");
if (s == NULL) {
return;
}
d = fopen(dest, "wb");
if (d == NULL) {
close(s);
return;
}
while (1) {
// c = getc(s); char at-a-time
// if (c == -1) {
if (fgets(line, 1000, s) == NULL) {
break;
}
// putc(c, d); char at-a-time
fputs(line, d);
}
fclose(s);
fclose(d);
}
main()
{
double t;
t = clock();
file_copy("0.dat", "1.dat");
printf("%.2f\n", (clock()-t) / (double)CLOCKS_PER_SEC);
}
Regards,
Rob Craig
Rapid Deployment Software
http://members.aol.com/FilesEu/