Newbie question: files..

Hrvoje Niksic hniksic at srce.hr
Sun Oct 10 16:25:41 EDT 1999


Sonny Parlin <sparlin at openpro.org> writes:

> WOW, now that's interesting! I never would have guessed something like
> that. Are you saying that perl code such as:
> 
> while(<FD>) {
>   print "$_\n";
> }
> 
> could be faster than low level C code such as:
> 
> while ((c = read (fd, buf, BUF_SIZE)) > 0)
> 	write (1, buf, c);

No way that can be true.  What Fredrik likely meant was that the Perl
code can be quicker than:

char buf[SIZE];
while (fgets (buf, sizeof buf, stdin))
  printf ("%s", buf);

...because fgets is atrociously slow.

> Not sure about that, or do you mean using C file pointers
> (i.e. stdio, as opposed to lower level methods (i.e. unistd/fcntl
> which is what my C example shows above))? I just tried those two
> pieces of code measuring time with /bin/time and the C version was
> consistently faster than the perl version...

Unless you are using extremely large files, /bin/time is not a good
way to measure the difference, because it also times Perl startup.

> Please keep in mind that I'm pretty ignorant to how Perl (or python
> for that matter) is implemented under the hood.

You probably don't want to know about Perl.  :-)  Python, on the other
hand, is very clean and very slow.




More information about the Python-list mailing list