简体   繁体   English

C ++程序在VS2008中运行缓慢

[英]C++ program runs slow in VS2008

I have a program written in C++, that opens a binary file(test.bin), reads it object by object, and puts each object into a new file (it opens the new file, writes into it(append), and closes it). 我有一个用C ++编写的程序,该程序打开一个二进制文件(test.bin),逐个对象读取它,并将每个对象放入一个新文件中(它打开新文件,写入新文件(附加),然后关闭它)。 I use fopen/fclose, fread and fwrite. 我使用fopen / fclose,fread和fwrite。 test.bin contains 20,000 objects. test.bin包含20,000个对象。

This program runs under linux with g++ in 1 sec but in VS2008 in debug/release mode in 1min! 该程序在带有g ++的linux下运行1秒钟,而在VS2008中以调试/发布模式运行1分钟!

There are reasons why I don't do them in batches or don't keep them in memory or any other kind of optimizations. 有一些原因导致我不批量执行或不将其保留在内存中或进行任何其他类型的优化。

I just wonder why it is that much slow under windows. 我只是想知道为什么在Windows下这么慢。

Thanks, 谢谢,

I believe that when you close a file in Windows, it flushes the contents to disk each time. 我相信在Windows中关闭文件时,每次都会将内容刷新到磁盘。 In Linux, I don't think that is the case . 在Linux中,我认为情况并非如此 The flush on each operation would be very expensive. 每次操作的冲洗将非常昂贵。

Unfortunately file access on Windows isn't renowned for its brilliant speed, particularly if you're opening lots of files and only reading and writing small amounts of data. 不幸的是,Windows上的文件访问并不以其出色的速度而著称,特别是如果您要打开大量文件并且仅读取和写入少量数据时。 For better results, the (not particularly helpful) solution would be to read large amounts of data from a small number of files. 为了获得更好的结果,(不是特别有用)的解决方案是从少量文件中读取大量数据。 (Or switch to Linux entirely for this program?!) (或完全为此程序切换到Linux ?!)

Other random suggestions to try: 其他随机建议尝试:

  • turn off the virus checker if you have one (I've got Kaspersky on my PC, and writing 20,000 files quickly drove it bananas) 如果有的话,请关闭病毒检查程序(我的PC上装有卡巴斯基,并且写入20,000个文件很快使它成为香蕉)
  • use an NTFS disk if you have one (FAT32 will be even worse) 如果有NTFS磁盘,则使用NTFS磁盘(FAT32甚至更糟)
  • make sure you're not accidentally using text mode with fopen (easily done) 确保您不会意外地将文本模式与fopen (轻松完成)
  • use setvbuf to increase the buffer size for each FILE 使用setvbuf增加每个FILE的缓冲区大小
  • try CreateFile / ReadFile /etc. 尝试CreateFile / ReadFile / ReadFile instead of fopen and friends, which won't solve your problem but may shave a few seconds off the running time (since the stdio functions do a bit of extra work that you probably don't need) 而不是fopen和friends,这不会解决您的问题,但可能会缩短运行时间几秒钟(因为stdio函数会执行一些您可能不需要的额外工作)

I think it is not matter of VS 2008. It is matter of Linux and Windows file system differences. 我认为这与VS 2008无关。这与Linux和Windows文件系统的差异有关。 And how C++ works with files in both systems. 以及C ++如何在两个系统中处理文件。

I'm seeing a lot of guessing here. 我在这里看到很多猜测。

You're running under VS2008 IDE. 您正在VS2008 IDE下运行。 You can always use the "poor man's profiler" and find out exactly what's going on. 您始终可以使用“穷人档案”, 准确地了解发生了什么。

In that minute, hit the "pause" button and look at what it's doing, including the call stack. 在那一刻,点击“暂停”按钮,查看它在做什么,包括调用堆栈。 Do this several times. 这样做几次。 Every single pause is almost certain (Prob = 59/60) to catch it doing precisely what it doesn't do under Linux. 几乎可以肯定每个停顿(Prob = 59/60)都可以使其完全在Linux下无法完成的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM