19

<?xml version通过 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.

也就是说,当我们"<?xml version"用(操作 2)替换字符串(操作 1)时"<?xml versioX",结果明显更快。第三个操作与第一个操作一样快,尽管它要多写两倍的字符。

任何人都可以重现这个吗?

Windows 7、32 位、MSVC 2010

编辑 1

在 R.. 建议后,禁用 Microsoft Security Essentials 可恢复正常行为。


如何使用类构造函数创建多个数组而不在类中创建多个数组?

我正在做一个运算符重载的练习。我创建了一个矩阵类,我应该重载运算符,这样我就可以有效地对矩阵进行算术运算。

我的指示说我应该使用具有 2 个参数的类构造函数和第三个矩阵数组创建两个矩阵数组,第三个矩阵数组将用于使用默认构造函数(1 个参数)存储算术结果。

由于我将使用这些数组来重载运算符,因此它们将需要成为该类的数据成员(我认为)。但是,我认为类应该尽可能地代表现实生活中的事物,因此制作具有多个数组的矩阵类对我来说没有意义(矩阵只是一个矩阵)。

我是否误解了类,或者是否有其他方法可以使用我没有想到的类构造函数来制作其他矩阵?谢谢大家,这是有问题的代码。

class matrix
{
    friend ostream& operator << (ostream&, const matrix&); // << overloader 

    private:
    int size // size indicates length of rows and cols, so size 3 means a 3 x 3 matrix
    int array[10][10];

    public:
    matrix(int);
    matrix(int, int);
};

matrix:: matrix (int sizeIn) //default constructor, use to make result matrix
{
    int MAX_SIZE = 10;

    if (0 > sizeIn && sizeIn > 10)
    {
     size = MAX_SIZE;
    }
    else
    {
     size = sizeIn;
    }

    for (int i = 0; i < size; i++)
        for (int j = 0; j < size; j++)
             array[i][j] = 0;
}

matrix:: matrix (int sizeIn, int rangeIn) //use to make first 2 matrices that will be added
{
    int range;
    int MAX_SIZE = 10;
    int MAX_RANGE = 20;

    if (0 > sizeIn && sizeIn > 10)
    {
     size = MAX_SIZE;
    }
    else
    {
     size = sizeIn;
    }

    if (0 > rangeIn && rangeIn > 20)
    {
      range = MAX_RANGE;
    }
    else
    {
     range = rangeIn;
    }

    for (int i = 0; i < size; i++)
        for (int j = 0; j < size; j++)
            array[i][j] = (rand() % (2 * range + 1) - range); //random number for each index
}

ostream & operator << (ostream & os, const matrix & arrayPrint) // << overloader
{
    for (int i = 0; i < arrayPrint.size; i++)
    {
        cout << '|';
        for (int j = 0; j < arrayPrint.size; j++)
            {
            os << setw(4) << arrayPrint.array[i][j] << " ";
            }
        os << setw(2) << '|' << endl;
    }
return os;
}
4

1 回答 1

27

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:

  • Only scan files once at transitions between writing and reading, not after every write. Even if you did write a virus to disk, it doesn't become a threat until it subsequently gets read by some process.
  • Once a file is scanned, remember that it's safe and don't scan it again until it's modified.
  • Only scan files that are executable programs or that are detected as being used as script/program-like data by another program.

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.

于 2011-05-07T23:55:01.933 回答