saurabh.hirani has asked for the wisdom of the Perl Monks concerning the following question:
I am working on a logging module. I was just studying the existing perl log modules and read that - syswrite is useful in a condition like logging as during its write operation it prevents someone else from writing at the same time. To verify that I wrote a small perl program
#!/usr/bin/perl -w use strict; use Fcntl; $| = 1; sub testme { my ($flag, $data) = @_; if ($flag == 1) { sysopen(FILE, 'bangbang', O_WRONLY | O_APPEND | O_CREAT); while (1) { syswrite FILE, "$data\n"; } } else { open(FILE, ">>bangbang"); while (1) { print FILE "$data\n"; } } close(FILE); } testme($ARGV[0], $ARGV[1]);
Testing both for syswrite and print - I found that $data printing in file is corrupted when I run 2 instances of the same program together. But using syswrite saves me from that. It has every line intact and no line is a mix of different data sets of the 2 programs.
But what struck me more was the size of the files created when I ran the following programs together:
this uses syswrite to write and check whether 'first' and 'second' clobber each other. During this run, I got the following stats:
Now for the print run,
this uses print to write and check whether 'first' and 'second' clobber each other. During this run, I got the following stats:
I was running a VM so I know that my profiling wouldn't be so accurate. But still, I had done autoflush for both syswrite and print. And I read somewhere that for output greater than 3 KB syswrite is faster?
Is print faster than syswrite? Is my testing flawed? Am I missing something? What would you use if you had to write a logging module in perl - print or syswrite? and why?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Is print faster than syswrite?
by ikegami (Patriarch) on Jan 27, 2009 at 13:54 UTC | |
by saurabh.hirani (Beadle) on Jan 28, 2009 at 05:26 UTC | |
by ikegami (Patriarch) on Jan 28, 2009 at 05:42 UTC | |
by saurabh.hirani (Beadle) on Jan 28, 2009 at 08:13 UTC | |
by belg4mit (Prior) on Jan 29, 2009 at 05:47 UTC | |
by ikegami (Patriarch) on Jan 29, 2009 at 07:23 UTC | |
by belg4mit (Prior) on Jan 30, 2009 at 03:47 UTC | |
by ikegami (Patriarch) on Jan 30, 2009 at 05:40 UTC | |
Re: Is print faster than syswrite?
by JavaFan (Canon) on Jan 27, 2009 at 13:43 UTC | |
by ikegami (Patriarch) on Jan 27, 2009 at 13:50 UTC | |
Re: Is print faster than syswrite?
by JavaFan (Canon) on Jan 27, 2009 at 14:25 UTC | |
by saurabh.hirani (Beadle) on Jan 28, 2009 at 05:02 UTC | |
Re: Is print faster than syswrite?
by zwon (Abbot) on Jan 27, 2009 at 20:09 UTC | |
by saurabh.hirani (Beadle) on Jan 28, 2009 at 05:19 UTC | |
Re: Is print faster than syswrite?
by jh- (Scribe) on Jan 27, 2009 at 13:50 UTC |