Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Seek, whence 0 or 1

by Hena (Friar)
on Jun 28, 2006 at 06:42 UTC ( [id://557945]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

This time the question is... *drumroll* Which is better to use in seek, whence 0 or 1?

while (<INPUT>) { if (m/$something/) { # so which one is better # seek(INPUT,$last,0) # seek(INPUT,tell(INPUT)-$last,1); } $last = tell(INPUT); }
Does it matter which to use? Is the second one slower than first? I think that the first is easier to read, but not much. Is the second slower as there is one more calculation? The second makes sense from a human point of view... browsing a book backwards for 5 pages is much faster than 1000 from the beginning (if one is in page 1005 and wants to end up in page 1000).

Yes, this is micro-optimization... but I'm still interested as I tend to use it :D. Most of the times, I've opted the first route though.

Replies are listed 'Best First'.
Re: Seek, whence 0 or 1
by GrandFather (Saint) on Jun 28, 2006 at 07:19 UTC

    Speed in this case is a complete non-issue. Any trivial difference there may be in calculating the file position will be completely swamped by the file systems' machinations in repositioning the file I/O pointer. Use whatever is clearest in intent and easiest to maintain. You will save orders of magnitude more time that way than any execution time that may be saved, or even the time spent pondering the question.


    DWIM is Perl's answer to Gödel
      I can quess that it doesn't help much. Mainly I'm interested on what are others opinions on which of the two seek lines is better (and tell also why and on what better) in the example above.

        Applying the test "which is easier to read and maintain" compare:

        seek (INPUT, $last, 0)

        and:

        seek (INPUT, tell (INPUT) - $last, 1);

        then tell me which is easier to read and which is easier to maintain (is less complex)? For me the absolute seek is the winner by far.

        Drawing an analogy with the way people seek in a book is not particuarly helpful as a model for analysing the pros and cons of the two seek methods by the way. Personally I tend to flip through a book from the back (seek (fh, pos, 2)) then perform a binaryish search from approximately the right place. :)


        DWIM is Perl's answer to Gödel
Re: Seek, whence 0 or 1
by shmem (Chancellor) on Jun 28, 2006 at 07:15 UTC
    It depends on how effective your code is to calculate either the next absolute position (0) or the offset (1). If , for instance, I have a file with sequential indices containing offsets, I'd use 1. If they are absolute file positions, then 0. Replacing with each other seek method doesn't make sense.
    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://557945]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 00:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found