Hi guys,

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:


1. ./program 1 first
2. ./program 1 second

this uses syswrite to write and check whether 'first' and 'second' clobber each other. During this run, I got the following stats:


1st run - 30 seconds - file size 40 MB
2nd run - 50 seconds - file size 61 MB

Now for the print run,


1. ./program 2 first
2. ./program 2 second

this uses print to write and check whether 'first' and 'second' clobber each other. During this run, I got the following stats:


1st run - 30 seconds - file size 315 MB
2nd run - 50 seconds - file size 586 MB

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?


Thanks for going through my post.

In reply to Is print faster than syswrite? by saurabh.hirani

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.