0

What is the overhead of calling the operating system some large amount of times?

For instance, Microsoft has an API called "Getpixel" You have to supply the x,y co-ordinates and it will return a colour value. Setpixel then has to make millions of requests to the OS.

What exactly is the overhead of doing this?

BigBug
  • 6,202
  • 23
  • 87
  • 138
  • It's low, sub-microsecond. The same cannot be said for GetPixel(), probably the slowest way to read 3 bytes. Read a bunch at a time, use BitBlt(). – Hans Passant Oct 25 '11 at 10:34

2 Answers2

2

Well for the example you give of GetPixel, it is slow because it uses a kernel mode driver to do the actual work, and in that driver it does a number of validation and locks to see if the device context you passed is actually a DC and to make sure it isn't changed somewhere in the function, then it makes a copy of an area into a new bitmap in memory and reads the pixel you want from that and after that deallocates the bitmap.

So you have a kernel mode switch, locks, validations and memory allocation, copying, and freeing and then another mode switch back to user land, all of which take time, finding a way to do GetPixel functionality in your program will save you tens of thousands of clock cycles.

But another API call may well cost no more than a few memory reads and writes, so it depends very much on which call into the OS you make.

Rinze Smits
  • 810
  • 6
  • 5
1

No universal answer. Depends on the call -e.g. calls involving I/O will be slower than those that don't-, depends on how the system calls are implemented by the OS -e.g. interrupts? jumps?- and depends on the architecture -e.g. does the architecture implements a system call instruction? does it do it by normal jumps?-.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • 1
    Hmm, i was wondering more generally actually. I'm not looking for a specific example or a specific amount of overhead. I'm more wondering why this overhead occurs and why we are attempting to spend as less amount of time within the OS as possible. I suppose the answer is in order to allow applications to run and do whatever it is they need to do without tying up the operating system by making un-necessary or ample calls to the OS.... Thanks for your response! – BigBug Oct 25 '11 at 08:18
  • 1
    Well the overhead includes what I said about how the system calls are implemented by the OS, if the OS takes advantage of the architecture special features for system calls (if available, like `syscall` instruction in MIPS architecture). Calling functions (be they system calls or inside your program) introduces overhead because you need to pass parameters, return values, and execute function entry/exit protocols. General functions considerations apply the same for system calls (e.g. reduce number of calls when possible, etc...). – m0skit0 Oct 25 '11 at 09:40