file size

Discussion in 'Perl Misc' started by George Mpouras, Jan 21, 2014.

  1. I have a file already open (in fact could be 100s). How can I get the
    size faster

    1) -f ...
    2) seek FILE, 0, 2; tell FILE;
     
    George Mpouras, Jan 21, 2014
    #1
    1. Advertisements

  2. Στις 21/1/2014 12:08, ο/η George Mpouras έγÏαψε:
    1) -s
     
    George Mpouras, Jan 21, 2014
    #2
    1. Advertisements

  3. use Benchmark;

    This code:

    #!/usr/bin/perl -w

    use Benchmark qw/cmpthese/;

    open FILE, '<file';
    cmpthese(10000000, {
    -s => sub { -s FILE },
    seektell => sub { seek FILE, 0, 2; tell FILE },
    })

    produces this output over here (Debian amd64, perl v5.18.2):

    Rate seektell -s
    seektell 3952569/s -- -48%
    -s 7575758/s 92% --

    So -s is almost twice as fast as seektell.

    If the seek is only done once, the result becomes:

    Rate -s seektell
    -s 7022472/s -- -82%
    seektell 39370079/s 461% --

    So tell is significantly faster than -s, but the combination of
    seek+tell is slower. Of course, YMMV. Run the benchmark and see what
    works best in your case.
    --
    Marius Gavrilescu

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1

    iQIcBAEBCgAGBQJS3l4bAAoJEMoENb5ewbNi5uoQAJJ0bWy8563Jb0F1AFrMIbLC
    tfTBwpuotCznakouOaB8d41H6vVeXrs9y0owtmYNg2SRCmfFoqPB2vc7wypF4J3j
    B7/UR2uoeFYvkWpqu0t7EjeozhtXv4Vo39JiTzJulzm48lp1mVk9Oj/azsa/hxTu
    hDK0zcWJL9q5CGk3ppBzpjrelS6iyx3tbbRHaOAyKY79DO4tr22JJFBhns7nAWih
    AB57p8/465IirAxYP3yrY3OABFsd2DUcjILJyASR9haTPU2MqvUIKLGU6rua/uwA
    7mXik8yEWrpbdIovO9XHKXqeNaGCCayNnobnpZiSZFuuvSxA9m+oP8/DS8M2vHSH
    z5yCcJbo3aQ8yk1OOugZ/U8slh59WzxG88Nme7uoWhjP/4qIek93pakrjGDJWuRm
    VWpuawTNXIgPhVyJsDQ9p95pBXCrO0yEzxC13RjDUzDCtDAOOe3Awhdd+TR3ztx1
    c4T7p81G7LSJiZqzujS9ITi6sphHz4YH0TfQbeoqnSW7Ba0ZWZgl6QQ61+hVeAKb
    i2PnunZL4mkXlQxJy/AqnDHS+u/5Vun5K2M+1DVr0xrPzaWsyiEWWtX3hpuJ0zh9
    9CyrsmCr0ggDosYFcww67NK/PIplo/SJAOjSBo6Y2YDDGkkpvIVQ8iSCivCJINot
    fsUKksOK1RcCInh21m45
    =bgO+
    -----END PGP SIGNATURE-----
     
    Marius Gavrilescu, Jan 21, 2014
    #3
  4. The first seek moves the current file position to the end of the
    file, which causes the old current position to be lost. All subsequent
    seeks don't do any actual seeking. This should rather be something like

    ---------
    #!/usr/bin/perl -w

    use Benchmark qw/cmpthese/;

    open FILE, '<file';
    cmpthese(10000000, {
    -s => sub {
    -s FILE
    },
    seektell => sub {
    my ($old, $rc);

    $old = tell(FILE);
    seek FILE, 0, 2;
    $rc = tell FILE;
    seek FILE, $old, 0;
    return $rc;
    },
    })
    ----------
     
    Rainer Weikusat, Jan 21, 2014
    #4
  5. It seems to me that a better implementation should be possible here but
    that's sort-of besides the point which was supposed to be that the first
    seek moves the file pointer to the end of the file and it then stays
    there for the purpose of this benchmark: There's nothing which magically
    causes it to revert to the 'current position' prior to the seek, hence


    [...]
    would be a fairer comparison because fstat doesn't destroy the current
    file position.
     
    Rainer Weikusat, Jan 21, 2014
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.