Performance difference between system call and library function - Unix

This is a discussion on Performance difference between system call and library function - Unix ; I'm trying to understand the performance difference between system call and methods provided in standard library. However I was confused by the result. I wrote a simple C program to compare the time used by the two methods. The two ...

+ Reply to Thread
Results 1 to 9 of 9

Thread: Performance difference between system call and library function

  1. Performance difference between system call and library function

    I'm trying to understand the performance difference between system
    call and methods provided in standard library. However I was confused
    by the result.

    I wrote a simple C program to compare the time used by the two
    methods. The two things I compared is:
    1. rename a file by system("mv file_a file_b") vs. by rename("file_a",
    "file_b")
    2. remove a file by system("rm file_b") vs. by remove("file_b")

    I was expecting the way that use system call take longer time, since
    the system call may invoke a shell in background and run the same
    command on that shell. However I found the two system calls were
    finished in less than 30 milliseconds, and the two library functions
    took around 200 milliseconds to finish.

    Is there anyone who can give me some comments on this? Thanks in
    advance.

  2. Re: Performance difference between system call and library function

    On Apr 16, 2:04 am, eliot wrote:

    > I'm trying to understand the performance difference between system
    > call and methods provided in standard library. However I was confused
    > by the result.


    Okay, so by "system call" you don't mean a call to the system (which
    is the normal usage). You mean the actual use of the "system"
    function, which is a method provided in the standard library.

    > I wrote a simple C program to compare the time used by the two
    > methods. The two things I compared is:
    > 1. rename a file by system("mv file_a file_b") vs. by rename("file_a",
    > "file_b")
    > 2. remove a file by system("rm file_b") vs. by remove("file_b")


    How did you measure the time used?

    > I was expecting the way that use system call take longer time, since
    > the system call may invoke a shell in background and run the same
    > command on that shell. However I found the two system calls were
    > finished in less than 30 milliseconds, and the two library functions
    > took around 200 milliseconds to finish.


    Something seems odd, since none of these operations should take
    anywhere near 200 milliseconds on any reasonably modern system.

    > Is there anyone who can give me some comments on this? Thanks in
    > advance.


    How did you get the times?

    You should not use the term "system call" to refer to the use of the
    "system" function. And you should *definitely* not use it this way to
    talk about "system call and library function" since the "system"
    function is a library function.

    You should use the term "system call" to refer to a call into the
    system, such as the 'read' function typically is. You should refer to
    the use of the "system" function as "using the 'system' function" or
    "executing a shell".

    DS

  3. Re: Performance difference between system call and library function

    use a system tool like truss or something like it to execute you code to
    find out what it is doing.


    "eliot" wrote in message
    news:07f102f5-4d71-48b1-ad87-afc8eadd47ab@2g2000hsn.googlegroups.com...
    > I'm trying to understand the performance difference between system
    > call and methods provided in standard library. However I was confused
    > by the result.
    >
    > I wrote a simple C program to compare the time used by the two
    > methods. The two things I compared is:
    > 1. rename a file by system("mv file_a file_b") vs. by rename("file_a",
    > "file_b")
    > 2. remove a file by system("rm file_b") vs. by remove("file_b")
    >
    > I was expecting the way that use system call take longer time, since
    > the system call may invoke a shell in background and run the same
    > command on that shell. However I found the two system calls were
    > finished in less than 30 milliseconds, and the two library functions
    > took around 200 milliseconds to finish.
    >
    > Is there anyone who can give me some comments on this? Thanks in
    > advance.




  4. Re: Performance difference between system call and library function

    Hi David,

    Thanks for your reply, and thank you for pointing out my misuse of
    "system call".

    You are right, I'm trying to compare the efficiency of using 'system'
    function to execute shell command 'rm' and using 'remove'/'unlink'
    functions to remove a file.

    Here is a small program I wrote in C to test the performance. The
    method I used to measure the time used by each function call is
    gettimeofday(), since it can reflect the difference in millisecond.

    #include
    #include
    #include

    int getelapsedtime(struct timeval starttime, struct timeval
    finishtime);

    int main()
    {
    struct timeval start, finish;
    int msec;

    system("cp bigfile bigfilea");
    system("cp bigfile bigfileb");
    system("cp bigfile bigfilec");

    gettimeofday(&start, NULL);
    system("rm bigfilea");
    gettimeofday(&finish, NULL);
    msec = getelapsedtime(start, finish);
    printf("Elapsed time for rm() is: %d milliseconds.\n", msec);

    gettimeofday(&start, NULL);
    remove("bigfileb");
    gettimeofday(&finish, NULL);
    msec = getelapsedtime(start, finish);
    printf("Elapsed time for remove() is: %d milliseconds.\n", msec);

    gettimeofday(&start, NULL);
    unlink("bigfilec");
    gettimeofday(&finish, NULL);
    msec = getelapsedtime(start, finishi);
    printf("Elapsed time for unlink() is: %d milliseconds.\n", msec);


    return(0);
    }

    int getelapsedtime(struct timeval starttime, struct timeval
    finishtime)
    {
    int msec = fini****ime.tv_sec * 1000 + finishtime.tv_usec / 1000;
    msec -= starttime.tv_sec * 1000 + starttime.tv_usec / 1000;
    return msec;
    }

    And this is the result of execution on solaris:
    # time mytimer
    Elapsed time for rm() is: 10 milliseconds.
    Elapsed time for remove() is: 1 milliseconds.
    Elapsed time for unlink() is: 1 milliseconds.

    real 0m0.181s
    user 0m0.030s
    sys 0m0.050s

  5. Re: Performance difference between system call and library function

    The result below is got from another Sun computer, and filesize I used
    is ~1M bytes. Previous result is got based on a file of size 400M
    bytes. Elapsed time for rm() is 149 milliseconds, and for unlink() is
    219 milliseconds.

    >
    > And this is the result of execution on solaris:
    > # time mytimer
    > Elapsed time for rm() is: 10 milliseconds.
    > Elapsed time for remove() is: 1 milliseconds.
    > Elapsed time for unlink() is: 1 milliseconds.
    >
    > real * * * *0m0.181s
    > user * * * 0m0.030s
    > sys * * * * 0m0.050s



  6. Re: Performance difference between system call and library function

    eliot wrote:
    > Hi David,
    >
    > Thanks for your reply, and thank you for pointing out my misuse of
    > "system call".


    I hope you know that you look like you're talking to yourself or an
    imaginary friend.


    > You are right, I'm trying to compare the efficiency of using 'system'
    > function to execute shell command 'rm' and using 'remove'/'unlink'
    > functions to remove a file.
    >
    > Here is a small program I wrote in C to test the performance. The
    > method I used to measure the time used by each function call is
    > gettimeofday(), since it can reflect the difference in millisecond.
    > [...]
    > And this is the result of execution on solaris:
    > # time mytimer
    > Elapsed time for rm() is: 10 milliseconds.
    > Elapsed time for remove() is: 1 milliseconds.
    > Elapsed time for unlink() is: 1 milliseconds.


    Thanks for the data. Did you have a question though?

  7. Re: Performance difference between system call and library function

    Well, just want to learn Unix programming, and the way Unix guys
    behave. I really appreciate one who give me advice.

    Forget to thank Neal, I'm using 'truss' to investigate my question,
    and the opposite results two machines returned. One finding to me is
    that 'remove' function seems call 'unlink' internally.

  8. Re: Performance difference between system call and library function

    eliot wrote:
    > Well, just want to learn Unix programming, and the way Unix guys
    > behave.


    You should learn how to post in Usenet first. It helps getting answers.
    Start with learning how to quote the person you reply to in your
    posts, because you still look like talking to yourself. I quoted a
    relevant part of your post in my reply. That way, others know that I'm
    actually answering to someone rather than talking to Mr. Invisible Friend.

  9. Re: Performance difference between system call and library function

    On Apr 16, 7:45 pm, eliot wrote:

    > int getelapsedtime(struct timeval starttime, struct timeval
    > finishtime)
    > {
    > int msec = fini****ime.tv_sec * 1000 + finishtime.tv_usec / 1000;
    > msec -= starttime.tv_sec * 1000 + starttime.tv_usec / 1000;
    > return msec;
    > }


    This function is *hugely* broken. Try something more like:

    int getelapsedtime(struct timeval starttime, struct timeval
    finishtime)
    {
    int msec=(finishtime.tv_sec-starttime.tv_sec)*1000;
    msec+=(finishtime.tv_usec-starttime.tv_usec)/1000;
    return msec;
    }

    When you do 'finishtime.tv_sec * 1000', you are engaging in insanity.
    You can't taken an arbitrary value, multiply it by 1,000, and assume
    it will fit in the same type it was originally in.

    DS

+ Reply to Thread