Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I have a program that opens and displays a text file size of 5MB to the browser , it is slow, I was wondering if there is a way to optimize this simple part of the code to make the file open fast than what it is right.
Any suggestion would be great!

# ***** Display selected log file to the screen ***** # #=comment print "<font face='Verdana, Arial, Helvetica, sans-serif' size=\"2\" c +olor=\"#000000\">"; open( FILE, "< $directory/$file" ) or die "Can't open $directory/$file + : $!"; while (my $display_file = <FILE>) { print "$display_file"; } print "</font>"; close FILE; #=cut


Thanks!

Replies are listed 'Best First'.
Re: Opening a Large Text File
by davorg (Chancellor) on Jul 06, 2006 at 15:10 UTC

    Maybe reading larger chunks of the file will help.

    { local $/ = \4096; # adjust as appropriate print while <FILE>; }
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Opening a Large Text File
by jeffa (Bishop) on Jul 06, 2006 at 15:23 UTC

    Since you invite any suggestions ... how about formatting the file itself and letting the web server handle opening and printing it? You could set up a cron job to add the font tag to the large file instead of trying to do so dynamically each request. Also, since you did not supply an example of what your 5 meg file looks like, are you really sure that whitespace is going to be handled the way you think it is?

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      This is just a plain text log file with lines like this one
      13:53:32,12-27-2004,Joe Bow,Toronto Travel .,ref_333,SSHTEST###12345## +#00000000000000, 9
        So based on that (typical?) line of log data, let's say it's 90 bytes/line, so a 5 MB file will be close to 56,000 lines of text.

        Now, what does the person looking at the browser intend to do with this display? Is the user looking for something in particular? Maybe just the oldest log entries, or just the newest ones, or just the ones that have some particular properties?

        If you figure out what the client needs to do with this information, you should focus on providing the particular subset of the log file that will directly support that task, so that you deliver an appropriate quantity of data, instead of delivering all the data.

        The appropriate quantity for manual inspection in a browser display will probably be a lot less than 5 MB (hopefully closer to 50 KB); then your task is to write the correct kind of filter that will deliver just that subset of data, and if the filter runs reasonably fast on the server, the slowness problem at the browser end will go probably away.

        If the client needs a complete download of the log file, then set it up as an ftp transfer to the client's local disk, instead of sending it to the browser display.

Re: Opening a Large Text File
by simon.proctor (Vicar) on Jul 06, 2006 at 15:39 UTC

    If the file is no longer being modified, you could gzip/zip it and offer that as an alternative if the browser supports compressed content (it will be in the accept part of the incoming request headers).

    You should get really good compression rates with plain text.

    If the file is still being modified then your choices for compression are a bit limited. Compressing on the fly *may* work but I wouldn't be surprised if the compressor needed to keep the file *as-is* while it ran. That opens up a whole new can of worms naturally :)

    HTH, S

Re: Opening a Large Text File
by holli (Abbot) on Jul 06, 2006 at 15:28 UTC
    You are printing 5 MB to the browser and it is slow? So where's the bottleneck?

    The bottleneck is clearly the net and/or the rendering speed of the browser, but not the IO of the script. Regardless how fast the net is, the harddisk is faster. So there's no need for optimization here. Well, maybe get a better server.


    holli, /regexed monk/
Re: Opening a Large Text File
by Moron (Curate) on Jul 06, 2006 at 15:16 UTC
    What I think is happening is you are splitting the I/O into very small peices and waiting for I/O over the net to complete in between reading or constructing every piece.

    Perhaps buffering your I/O will help, e.g.:

    # ***** Display selected log file to the screen ***** # #=comment my @buffer = "<font face='Verdana, Arial, Helvetica, sans-serif' size= +\"2\" color=\"#000000\">"; open( FILE, "< $directory/$file" ) or die "Can't open $directory/$file + : $!"; push @buffer, <FILE>; close FILE; push @buffer, "</font>"; print @buffer; #=cut

    -M

    Free your mind

Re: Opening a Large Text File
by McDarren (Abbot) on Jul 06, 2006 at 16:06 UTC
    You didn't mention which webserver you are running (or if you even have administrative control over it), but....

    ..if it happens to be Apache, and you DO have administrative control - then you might want to consider mod_deflate. I've used it before and seen significant performance improvments - particulary with pages that are mostly text.

    HTH,
    Darren :)