程序在我的机器上运行 2 秒,但在其他机器上运行 15 秒

发布于 2024-08-02 13:04:04 字数 1013 浏览 5 评论 0原文

我有两个用 C++ 编写的使用 Winsock 的程序。它们都接受 TCP 连接,一个发送数据,另一个接收数据。它们是在 Visual Studio 2008 中编译的。我还有一个用 C# 编写的程序,它连接到两个 C++ 程序并转发从一个程序接收的数据包并将其发送到另一个程序。在此过程中,它会计算并显示转发的数据包数量。此外,还显示从第一个数据包到最近的数据包所经过的时间。

发送数据包的 C++ 程序只是循环 1000 次发送完全相同的数据。当我在开发机器上运行所有三个应用程序(使用环回或实际 IP)时,数据包在大约 2 秒内穿过整个系统。当我在实验室中的任何其他 PC 上运行这三个程序时,总是需要 15 到 16 秒的时间。每台 PC 都有不同的处理器和内存量,但它们都运行 Windows XP Professional。我的开发 PC 实际上有一台较旧的 AMD Athlon,其内存只有执行此任务所需时间较长的一台机器的一半。我在我的机器和另一台机器上的任务管理器中观察了 CPU 时间图,在这些程序运行时,它们都没有使用大量处理器(即超过 10%)。

有人有什么想法吗?我只能考虑在目标计算机上安装 Visual Studio,看看是否与此有关。

问题已解决================================================== ====

我首先安装了 Visual Studio,看看是否有任何效果,但没有。然后我在新的开发电脑上测试了这些程序,它的运行速度和我的旧电脑一样快。在 Vista 笔记本电脑上运行程序的结果再次为 15 秒。

我在服务器程序中某些指令的两侧打印了时间戳,以查看哪条指令花费的时间最长,我发现延迟是由 1 毫秒的 Sleep() 方法调用引起的。显然,在我的新旧系统上,睡眠(1)被忽略,因为我会在同一毫秒内发送 10 到 > 20 个数据包。有时我会在执行过程中出现大约 15 或 16 毫秒的中断,这导致 1000 个数据包的时间约为 2 秒。在需要大约 15 秒运行完 1000 个数据包的系统上,发送每个数据包之间会有 15 或 16 毫秒的间隔。

我注释掉了 Sleep() 方法调用,现在数据包立即发送。感谢您的帮助。

I have two programs written in C++ that use Winsock. They both accept TCP connections and one sends data the other receives data. They are compiled in Visual Studio 2008. I also have a program written in C# that connects to both C++ programs and forwards the packets it receives from one and sends them to the other. In the process it counts and displays the number of packets forwarded. Also, the elapsed time from the first to the most recent packet is displayed.

The C++ program that sends packets simply loops 1000 times sending the exact same data. When I run all three apps on my development machine (using loopback or actual IP) the packets get run through the entire system in around 2 seconds. When I run all three on any other PC in our lab it always takes between 15 and 16 seconds. Each PC has different processors and amounts of memory but all of them run Windows XP Professional. My development PC actually has an older AMD Athlon with half as much memory as one of the machines that takes longer to perform this task. I have watched the CPU time graph in Task Manager on my machine and one other and neither of them is using a significant amount of the processor (i.e. more than 10%) while these programs run.

Does anyone have any ideas? I can only think to install Visual Studio on a target machine to see if it has something to do with that.

Problem Solved ====================================================

I first installed Visual Studio to see if that had any effect and it didn't. Then I tested the programs on my new development PC and it ran just as fast as my old one. Running the programs on a Vista laptop yielded 15 second times again.

I printed timestamps on either side of certain instructions in the server program to see which was taking the longest and I found that the delay was being caused by a Sleep() method call of 1 millisecond. Apparently on my old and new systems the Sleep(1) was being ignored because I would have anywhere from 10 to >20 packets being sent in the same millisecond. Occasionally I would have a break in execution of around 15 or 16 milliseconds which led to the the time of around 2 seconds for 1000 packets. On the systems that took around 15 seconds to run through 1000 packets I would have either a 15 or 16 millisecond gap between sending each packet.

I commented out the Sleep() method call and now the packets get sent immediately. Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

早茶月光 2024-08-09 13:04:04

您应该在良好的 2 秒案例和 15 秒实验室案例上分析您的应用程序,看看它们有何不同。差异可能是由于许多问题(磁盘、防病毒、网络)造成的 - 没有任何数据支持,我们只是在黑暗中拍摄。

如果您无法访问分析器,则可以将计时仪器添加到程序的各个阶段,以查看哪个阶段花费的时间更长。

You should profile your application on the good, 2 second case, and the 15 second lab case and see where they differ. The difference could be due to any number of a problems (disk, antivirus, network) - without any data backing it up we'd just be shooting in the dark.

If you don't have access to a profiler, you can add timing instrumentation to various phases of your program to see which phase is taking longer.

站稳脚跟 2024-08-09 13:04:04

您可以尝试检查 Winsock 性能调整注册表设置 - 可能是某些游戏或实用程序的安装对您电脑上的内容进行了调整。

You could try checking the Winsock performance tuning registry settings - it may be the installation of some game or utility has tweaked those on your PC.

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