调用操作系统的开销
多次调用操作系统的开销是多少?
例如,微软有一个名为“Getpixel”的 API,您必须提供 x,y 坐标,它将返回一个颜色值。然后 Setpixel 必须向操作系统发出数百万个请求。
这样做的开销到底是多少?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于您给出的 GetPixel 示例,它很慢,因为它使用内核模式驱动程序来完成实际工作,并且在该驱动程序中,它会执行大量验证和锁定,以查看您传递的设备上下文是否实际上是 DC 和为了确保它不会在函数中的某个地方发生更改,然后它将一个区域复制到内存中的一个新位图中,并从中读取所需的像素,然后释放该位图。
因此,您有一个内核模式切换、锁定、验证和内存分配、复制和释放,然后另一个模式切换回用户态,所有这些都需要时间,找到一种在程序中执行 GetPixel 功能的方法将为您节省数十美元数千个时钟周期。
但是另一个 API 调用的成本很可能不会超过几次内存读取和写入,因此这在很大程度上取决于您对操作系统进行的调用。
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.
没有普遍的答案。取决于调用 - 例如涉及 I/O 的调用会比不涉及 I/O 的调用慢 - 取决于操作系统如何实现系统调用 - 例如中断?跳转?-并且取决于体系结构-例如体系结构是否实现系统调用指令?它是通过正常的跳跃做到的吗?-.
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?-.