Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comparison between system operators and Built in function

by jesuashok (Curate)
on Jan 19, 2006 at 02:49 UTC ( #524108=perlquestion: print w/ replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

Hi All !!!

chdir

opendir

readdir

closedir

symlink

.. etc

Above are examples system operators.

we have equivalent built in functions also.

open

read

close

.. etc

Among the above which one will be faster, so that I can avoid some performance Issue in the code

"Keep pouring your ideas"

Comment on comparison between system operators and Built in function
Re: comparison between system operators and Built in function
by dragonchild (Archbishop) on Jan 19, 2006 at 03:18 UTC
    Benchmark, specifically cmpthese().

    Also, opendir and open don't do the same thing. You sure you're not prematurely optimizing?


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

      Benchmarking issues apart, not only opendir and open don't do the same thing: more precisely the OP gives two lists of items presenting them, for some strange reason, as "examples system operators" and "built in functions" respectively. But no item from the first one is equivalent to any of the second one for any reasonable acceptation of "equivalent"!

      Also, all of those items represent a built in functions with that name, that actually do system calls. So I really don't know what he may have in mind, but I wonder how he could even cmpthese() stuff that bears no resemblance to each other. Maybe he's not (yet) prematurely optimizing, but he seems definitely to be concerned about premature optimization, which we all know, is Bad(TM).

Re: comparison between system operators and Built in function
by stonecolddevin (Vicar) on Jan 19, 2006 at 03:21 UTC
    each of those really have their own purpose, and unless you're using something like sysread() or sysopen() none of them should really be subsituted for another.
    meh.
Re: comparison between system operators and Built in function
by serf (Chaplain) on Jan 19, 2006 at 03:26 UTC
    Hi jesuashok where do you get "equivalent built in functions" between those two lists?
    • chdir
    • - Changes the working directory to EXPR - you can't do this with system("cd $dir"); or $ENV{PWD} = $dir; - chdir looks like a good option :o)
    • opendir
    • - Opens a directory named EXPR for processing by readdir, telldir, seekdir, rewinddir, and closedir. - what else could you use to do that?
    • readdir
    • - Returns the next directory entry for a directory opened by opendir. I guess you could fudge that up by using ls, but using readdir from within Perl is more simple and efficient.
    • closedir
    • - Closes a directory opened by opendir - how could you do that?
    • symlink
    • - sure, you could do system ("ln -s $orig $link"); but why complicate things, and why ask someone else to do something you can easily do yourself and then know the result straight away?
    • open
    • - there are many ways to read data out of file, using open within perl is certainly a good one. Using backticks and cat or something like that would not be likely to win you any "well done" prizes.
    • read
    • - Attempts to read LENGTH characters of data into variable SCALAR from the specified FILEHANDLE.
    • close
    • - Closes the file or pipe associated with the file handle - again, what are you proposing to do this with?
Re: comparison between system operators and Built in function
by vennirajan (Friar) on Jan 19, 2006 at 04:44 UTC
    Hi,

        When some one wants to you use the OS related operations, they will try to use "system" function. It will be easier for them to use. But, when you use those functions, it will be executed by the shell, and the value will be returned. That will definitely increase system load. Instead of that you can use the builtin functions which is available in perl itself. But, here you need to find out the appropriate module and have to use the correct function. When you go for the builtin functions, it wont take much time compared to the system functions.



    Regards,
    S.Venni Rajan.
    "A Flair For Excellence."
                    -- BK Systems.
      But, when you use those functions, it will be executed by the shell, and the value will be returned.

      Really?

      q:~ [10:07:26]$ ps U blazar PID TTY STAT TIME COMMAND 11831 ? S 0:00 sshd: blazar@pts/0 11838 pts/0 Ss 0:00 -bash 11857 pts/0 R+ 0:00 ps U blazar q:~ [10:07:32]$ perl -e 'system qw/sleep 600/' [1]+ Stopped perl -e 'system qw/sleep 600/' q:~ [10:08:09]$ ps U blazar PID TTY STAT TIME COMMAND 11831 ? S 0:00 sshd: blazar@pts/0 11838 pts/0 Ss 0:00 -bash 11908 pts/0 T 0:00 perl -e system qw/sleep 600/ 11909 pts/0 T 0:00 sleep 600 11922 pts/0 R+ 0:00 ps U blazar

      I don't see additional shells...

      (To the benefit of the OP) this "detail" apart, indeed system calls are expensive.

Re: comparison between system operators and Built in function
by Nude Reaper (Initiate) on Jan 19, 2006 at 06:59 UTC

    Perhaps you could benchmark this and report back.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2014-04-16 23:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (436 votes), past polls