调用操作系统的开销

发布于 2024-12-11 18:18:56 字数 129 浏览 0 评论 0原文

多次调用操作系统的开销是多少?

例如,微软有一个名为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煮酒 2024-12-18 18:18:56

对于您给出的 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.

谈下烟灰 2024-12-18 18:18:56

没有普遍的答案。取决于调用 - 例如涉及 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?-.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文