Results 1 to 7 of 7

Thread: Functions or seperate scripts?

  1. #1
    Join Date
    Jun 2012
    Location
    Sweden
    Beans
    324
    Distro
    Ubuntu 12.04 Precise Pangolin

    Functions or seperate scripts?

    Hi!
    I currently have a separate file with all my aliases and functions that I source via .bashrc. But would separate scripts be faster (in e.g. ~/bin)?
    Functions are, as I understand, loaded into the RAM and because of that, functions are very fast. An other advantage is that functions is executed in the current shell, which can be handy in some cases. And on the other side, scripts are only loaded when you run them, but the computer have to read from the disk every time you call a new script. All functions are in the RAM from beginning, and the computer don't need to read from the disk. Scripts often starts with comments and #! to tell Bash what shell it should use. Functions in Bash always run in Bash, so there's no need to find out which shell the script want to use, nor start a new subshell to run the script in.
    What do you prefer and what is the fastest and best way?

  2. #2
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,005
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: Functions or seperate scripts?

    There is no fast rule. On any Unix system the file system is quite fast anyway, and the overhead of reading the script file is minimal. Functions could have a speed advantage is you use a lot of for loops at the console, but that would hold only if the functions are 100% bash with no calls to utilities (rare).

  3. #3

    Re: Functions or seperate scripts?

    Another difference is that scripts can be invoked from shells different from what they were written in, or even from other programs. Consequently, there are things you can do with a script that you can't do with a function, like monitor it with `watch` or run it with `find -exec`.

  4. #4
    Join Date
    Jun 2012
    Location
    Sweden
    Beans
    324
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Functions or seperate scripts?

    Quote Originally Posted by trent.josephsen View Post
    Another difference is that scripts can be invoked from shells different from what they were written in, or even from other programs. Consequently, there are things you can do with a script that you can't do with a function, like monitor it with `watch` or run it with `find -exec`.
    As far as I know, watch are using sh to run commands. And since sh doesn't support shell functions, watch doesn't work with shell functions, either.
    But I asked what the fastest way was. I have quite much own-written code now, and I just wonder how to make it as fast as possible.

  5. #5
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,005
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: Functions or seperate scripts?

    Quote Originally Posted by Bufeu View Post
    I have quite much own-written code now, and I just wonder how to make it as fast as possible.
    Rewrite it in C...

  6. #6
    Join Date
    Jun 2012
    Location
    Sweden
    Beans
    324
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Functions or seperate scripts?

    So, both ways are equally fast in modern UNIX-systems?

  7. #7
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,005
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: Functions or seperate scripts?

    Quote Originally Posted by Bufeu View Post
    So, both ways are equally fast in modern UNIX-systems?
    I don't think you will ever notice the difference in real use, unless you are trying to execute them in a tight loop with very many iterations...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •