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?
In reply to Is print faster than syswrite? by saurabh.hirani
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |