fwrite 在“
当通过fwrite将字符串写入文件时,后续的写入操作会变慢。
这段代码:
#include <cstdio>
#include <ctime>
#include <iostream>
int main()
{
const long index(15000000);
clock_t start_time(clock());
FILE* file_stream1 = fopen("test1.txt","wb");
fwrite("<?xml version",1,13,file_stream1);
for(auto i = 1;i < index ;++i)
fwrite("only 6",1,6,file_stream1);
fclose(file_stream1);
std::cout << "\nOperation 1 took : "
<< static_cast<double>(clock() - start_time)/CLOCKS_PER_SEC
<< " seconds.";
start_time = clock();
FILE* file_stream2 = fopen("test2.txt","wb");
fwrite("<?xml versioX",1,13,file_stream2);
for(auto i = 1;i < index ;++i)
fwrite("only 6",1,6,file_stream2);
fclose(file_stream2);
std::cout << "\nOperation 2 took : "
<< static_cast<double>(clock() - start_time)/CLOCKS_PER_SEC
<< " seconds.";
start_time = clock();
FILE* file_stream3 = fopen("test3.txt","w");
const char test_str3[] = "<?xml versioX";
for(auto i = 1;i < index ;++i)
fwrite(test_str3,1,13,file_stream3);
fclose(file_stream3);
std::cout << "\nOperation 3 took : "
<< static_cast<double>(clock() - start_time)/CLOCKS_PER_SEC
<< " seconds.\n";
return 0;
}
给了我这个结果:
Operation 1 took : 3.185 seconds.
Operation 2 took : 2.025 seconds.
Operation 3 took : 2.992 seconds.
那就是当我们将字符串 " (操作 1)替换为 " (操作2)结果明显更快。第三个操作与第一个操作一样快,尽管它多写了两倍的字符。
任何人都可以复制这个吗?
Windows 7、32 位、MSVC 2010
编辑 1
根据 R.. 建议,禁用 Microsoft Security Essentials 可恢复正常行为。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web
技术交流群。
当通过fwrite将字符串写入文件时,后续的写入操作会变慢。
这段代码:
#include <cstdio>
#include <ctime>
#include <iostream>
int main()
{
const long index(15000000);
clock_t start_time(clock());
FILE* file_stream1 = fopen("test1.txt","wb");
fwrite("<?xml version",1,13,file_stream1);
for(auto i = 1;i < index ;++i)
fwrite("only 6",1,6,file_stream1);
fclose(file_stream1);
std::cout << "\nOperation 1 took : "
<< static_cast<double>(clock() - start_time)/CLOCKS_PER_SEC
<< " seconds.";
start_time = clock();
FILE* file_stream2 = fopen("test2.txt","wb");
fwrite("<?xml versioX",1,13,file_stream2);
for(auto i = 1;i < index ;++i)
fwrite("only 6",1,6,file_stream2);
fclose(file_stream2);
std::cout << "\nOperation 2 took : "
<< static_cast<double>(clock() - start_time)/CLOCKS_PER_SEC
<< " seconds.";
start_time = clock();
FILE* file_stream3 = fopen("test3.txt","w");
const char test_str3[] = "<?xml versioX";
for(auto i = 1;i < index ;++i)
fwrite(test_str3,1,13,file_stream3);
fclose(file_stream3);
std::cout << "\nOperation 3 took : "
<< static_cast<double>(clock() - start_time)/CLOCKS_PER_SEC
<< " seconds.\n";
return 0;
}
给了我这个结果:
Operation 1 took : 3.185 seconds.
Operation 2 took : 2.025 seconds.
Operation 3 took : 2.992 seconds.
那就是当我们将字符串 " (操作 1)替换为
" (操作2)结果明显更快。第三个操作与第一个操作一样快,尽管它多写了两倍的字符。
任何人都可以复制这个吗?
Windows 7、32 位、MSVC 2010
编辑 1
根据 R.. 建议,禁用 Microsoft Security Essentials 可恢复正常行为。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Windows 上,大多数(全部?)防病毒软件的工作原理是挂钩文件读取和/或写入操作,以再次运行正在读取或写入的数据病毒模式并将其分类为安全或病毒。我怀疑您的防病毒软件一旦看到 XML 标头,就会加载 XML 恶意软件病毒模式,并从那时起开始不断检查您写入磁盘的 XML 是否是已知病毒的一部分。
当然,这种行为完全是无稽之谈,也是 AV 程序在有能力的用户中声誉不佳的部分原因,他们一打开 AV 就会发现自己的性能直线下降。可以通过其他不影响性能的方式来实现相同的目标。以下是他们应该使用的一些想法:
不幸的是,在 AV 软件制造商明智之前,我不知道有任何解决方法,除了关闭 AV 之外......这在 Windows 上通常是一个坏主意。
On Windows, most (all?) anti-virus software works by hooking into the file read and/or write operations to run the data being read or written again virus patterns and classify it as safe or virus. I suspect your anti-virus software, once it sees an XML header, loads up the XML-malware virus patterns and from that point on starts constantly checking to see if the XML you're writing to disk is part of a known virus.
Of course this behavior is utterly nonsensical and is part of what gives AV programs such a bad reputation with competent users, who see their performance plummet as soon as they turn on AV. The same goal could be accomplished in other ways that don't ruin performance. Here are some ideas they should be using:
Unfortunately I don't know of any workaround until AV software makers wise up, other than turning your AV off... which is generally a bad idea on Windows.