我是一个新用户,我的英文不太好,所以我希望能够清楚。 当你试图扩大它们的尺寸时,我们正面临着使用大文件(1GB或更多)的性能问题,尤其是(看起来)。Windows上C++的I/O性能差异很大
反正...来验证我们的感觉,我们tryed以下
一个(在Win 7 64位,4core,8GB的内存,32位与VC2008编译的代码))打开一个unexisting文件。从1Mb插槽开始写入到1Gb。
现在你有一个1Gb文件。
现在随机化该文件中的10000个位置,寻找该位置并在每个位置写入50个字节,无论您写什么。
关闭文件并查看结果。
创建该文件的时间相当快(约为0.3“),写入10000次的时间总是相同(约为0.03”)。
非常好,这是beginnig。
现在尝试其他的东西...
b)打开一个不存在的文件,寻找1Gb-1字节,只写1个字节。
现在你有另一个1Gb文件。
按照下面的步骤完全相同的方式处理'a',关闭文件并查看结果。
时间创建的文件是你能想象的更快(约0.00009"),但写的时候是你无法相信.... 90" !!!!!
B.1)打开一个文件unexisting,不写任何字节。
像以前一样行动起来,寻求和写作,关闭文件并看看结果。
时间写长都是一样的:90" !!!!!
好吧......这是相当惊人的,但再有更多的
C)打开文件!你创造了'a'的情况下,不要截断它...再次随机化10000个位置并像以前一样行动,你的速度与之前一样,大约为0,03“”写入10000次。
听起来不错...尝试另一个步骤。
d)现在打开你的情况下,“B”创建的文件,也不会截断它...再次随机位置10000和之前采取行动。你慢一遍又一遍,但时间减少到...... 45" !!也许,试图再次,时间将减少。
其实我不知道为什么...... 任何想法?
以下是我用来测试我在previuos案例中所说的代码的一部分(为了有一个干净的编译,我只是从一些源代码中剪下&粘贴,对不起)。
样本可以随机读取和写入,有序或反向排序模式,但只写入随机顺序是最清晰的测试。
我们尝试过使用std :: fstream,但也直接使用CreateFile(),WriteFile()等等结果是一样的(即使std :: fstream实际上慢一点)。
参数的情况下的 'A'=> -f_tempdir_ \ casea.dat -n10000 -t -p -w
参数情况下 'B'=> -f_tempdir_ \ caseb.dat -n10000 -t -v -w
参数情况下 'B.1'=> -f_tempdir_ \ caseb.dat -n10000 -t -w
参数情况下的 'C'=> -f_tempdir_ \ casea.dat -n10000 -w
参数的情况下' d” => -f_tempdir_ \ caseb.dat -n10000 -w
运行测试(甚至其他)和见...
// iotest.cpp : Defines the entry point for the console application.
//
#include <windows.h>
#include <iostream>
#include <set>
#include <vector>
#include "stdafx.h"
double RealTime_Microsecs()
{
LARGE_INTEGER fr = {0, 0};
LARGE_INTEGER ti = {0, 0};
double time = 0.0;
QueryPerformanceCounter(&ti);
QueryPerformanceFrequency(&fr);
time = (double) ti.QuadPart/(double) fr.QuadPart;
return time;
}
int main(int argc, char* argv[])
{
std::string sFileName ;
size_t stSize, stTimes, stBytes ;
int retval = 0 ;
char *p = NULL ;
char *pPattern = NULL ;
char *pReadBuf = NULL ;
try {
// Default
stSize = 1<<30 ; // 1Gb
stTimes = 1000 ;
stBytes = 50 ;
bool bTruncate = false ;
bool bPre = false ;
bool bPreFast = false ;
bool bOrdered = false ;
bool bReverse = false ;
bool bWriteOnly = false ;
// Comsumo i parametri
for(int index=1; index < argc; ++index)
{
if ('-' != argv[index][0]) throw ;
switch(argv[index][1])
{
case 'f': sFileName = argv[index]+2 ;
break ;
case 's': stSize = xw::str::strtol(argv[index]+2) ;
break ;
case 'n': stTimes = xw::str::strtol(argv[index]+2) ;
break ;
case 'b':stBytes = xw::str::strtol(argv[index]+2) ;
break ;
case 't': bTruncate = true ;
break ;
case 'p' : bPre = true, bPreFast = false ;
break ;
case 'v' : bPreFast = true, bPre = false ;
break ;
case 'o' : bOrdered = true, bReverse = false ;
break ;
case 'r' : bReverse = true, bOrdered = false ;
break ;
case 'w' : bWriteOnly = true ;
break ;
default: throw ;
break ;
}
}
if (sFileName.empty())
{
std::cout << "Usage: -f<File Name> -s<File Size> -n<Number of Reads and Writes> -b<Bytes per Read and Write> -t -p -v -o -r -w" << std::endl ;
std::cout << "-t truncates the file, -p pre load the file, -v pre load 'veloce', -o writes in order mode, -r write in reverse order mode, -w Write Only" << std::endl ;
std::cout << "Default: 1Gb, 1000 times, 50 bytes" << std::endl ;
throw ;
}
if (!stSize || !stTimes || !stBytes)
{
std::cout << "Invalid Parameters" << std::endl ;
return -1 ;
}
size_t stBestSize = 0x00100000 ;
std::fstream fFile ;
fFile.open(sFileName.c_str(), std::ios_base::binary|std::ios_base::out|std::ios_base::in|(bTruncate?std::ios_base::trunc:0)) ;
p = new char[stBestSize] ;
pPattern = new char[stBytes] ;
pReadBuf = new char[stBytes] ;
memset(p, 0, stBestSize) ;
memset(pPattern, (int)(stBytes&0x000000ff), stBytes) ;
double dTime = RealTime_Microsecs() ;
size_t stCopySize, stSizeToCopy = stSize ;
if (bPre)
{
do {
stCopySize = std::min(stSizeToCopy, stBestSize) ;
fFile.write(p, stCopySize) ;
stSizeToCopy -= stCopySize ;
} while (stSizeToCopy) ;
std::cout << "Creating time is: " << xw::str::itoa(RealTime_Microsecs()-dTime, 5, 'f') << std::endl ;
}
else if (bPreFast)
{
fFile.seekp(stSize-1) ;
fFile.write(p, 1) ;
std::cout << "Creating Fast time is: " << xw::str::itoa(RealTime_Microsecs()-dTime, 5, 'f') << std::endl ;
}
size_t stPos ;
::srand((unsigned int)dTime) ;
double dReadTime, dWriteTime ;
stCopySize = stTimes ;
std::vector<size_t> inVect ;
std::vector<size_t> outVect ;
std::set<size_t> outSet ;
std::set<size_t> inSet ;
// Prepare vector and set
do {
stPos = (size_t)(::rand()<<16) % stSize ;
outVect.push_back(stPos) ;
outSet.insert(stPos) ;
stPos = (size_t)(::rand()<<16) % stSize ;
inVect.push_back(stPos) ;
inSet.insert(stPos) ;
} while (--stCopySize) ;
// Write & read using vectors
if (!bReverse && !bOrdered)
{
std::vector<size_t>::iterator outI, inI ;
outI = outVect.begin() ;
inI = inVect.begin() ;
stCopySize = stTimes ;
dReadTime = 0.0 ;
dWriteTime = 0.0 ;
do {
dTime = RealTime_Microsecs() ;
fFile.seekp(*outI) ;
fFile.write(pPattern, stBytes) ;
dWriteTime += RealTime_Microsecs() - dTime ;
++outI ;
if (!bWriteOnly)
{
dTime = RealTime_Microsecs() ;
fFile.seekg(*inI) ;
fFile.read(pReadBuf, stBytes) ;
dReadTime += RealTime_Microsecs() - dTime ;
++inI ;
}
} while (--stCopySize) ;
std::cout << "Write time is " << xw::str::itoa(dWriteTime, 5, 'f') << " (Ave: " << xw::str::itoa(dWriteTime/stTimes, 10, 'f') << ")" << std::endl ;
if (!bWriteOnly)
{
std::cout << "Read time is " << xw::str::itoa(dReadTime, 5, 'f') << " (Ave: " << xw::str::itoa(dReadTime/stTimes, 10, 'f') << ")" << std::endl ;
}
} // End
// Write in order
if (bOrdered)
{
std::set<size_t>::iterator i = outSet.begin() ;
dWriteTime = 0.0 ;
stCopySize = 0 ;
for(; i != outSet.end(); ++i)
{
stPos = *i ;
dTime = RealTime_Microsecs() ;
fFile.seekp(stPos) ;
fFile.write(pPattern, stBytes) ;
dWriteTime += RealTime_Microsecs() - dTime ;
++stCopySize ;
}
std::cout << "Ordered Write time is " << xw::str::itoa(dWriteTime, 5, 'f') << " in " << xw::str::itoa(stCopySize) << " (Ave: " << xw::str::itoa(dWriteTime/stCopySize, 10, 'f') << ")" << std::endl ;
if (!bWriteOnly)
{
i = inSet.begin() ;
dReadTime = 0.0 ;
stCopySize = 0 ;
for(; i != inSet.end(); ++i)
{
stPos = *i ;
dTime = RealTime_Microsecs() ;
fFile.seekg(stPos) ;
fFile.read(pReadBuf, stBytes) ;
dReadTime += RealTime_Microsecs() - dTime ;
++stCopySize ;
}
std::cout << "Ordered Read time is " << xw::str::itoa(dReadTime, 5, 'f') << " in " << xw::str::itoa(stCopySize) << " (Ave: " << xw::str::itoa(dReadTime/stCopySize, 10, 'f') << ")" << std::endl ;
}
}// End
// Write in reverse order
if (bReverse)
{
std::set<size_t>::reverse_iterator i = outSet.rbegin() ;
dWriteTime = 0.0 ;
stCopySize = 0 ;
for(; i != outSet.rend(); ++i)
{
stPos = *i ;
dTime = RealTime_Microsecs() ;
fFile.seekp(stPos) ;
fFile.write(pPattern, stBytes) ;
dWriteTime += RealTime_Microsecs() - dTime ;
++stCopySize ;
}
std::cout << "Reverse ordered Write time is " << xw::str::itoa(dWriteTime, 5, 'f') << " in " << xw::str::itoa(stCopySize) << " (Ave: " << xw::str::itoa(dWriteTime/stCopySize, 10, 'f') << ")" << std::endl ;
if (!bWriteOnly)
{
i = inSet.rbegin() ;
dReadTime = 0.0 ;
stCopySize = 0 ;
for(; i != inSet.rend(); ++i)
{
stPos = *i ;
dTime = RealTime_Microsecs() ;
fFile.seekg(stPos) ;
fFile.read(pReadBuf, stBytes) ;
dReadTime += RealTime_Microsecs() - dTime ;
++stCopySize ;
}
std::cout << "Reverse ordered Read time is " << xw::str::itoa(dReadTime, 5, 'f') << " in " << xw::str::itoa(stCopySize) << " (Ave: " << xw::str::itoa(dReadTime/stCopySize, 10, 'f') << ")" << std::endl ;
}
}// End
dTime = RealTime_Microsecs() ;
fFile.close() ;
std::cout << "Flush/Close Time is " << xw::str::itoa(RealTime_Microsecs()-dTime, 5, 'f') << std::endl ;
std::cout << "Program Terminated" << std::endl ;
}
catch(...)
{
std::cout << "Something wrong or wrong parameters" << std::endl ;
retval = -1 ;
}
if (p) delete []p ;
if (pPattern) delete []pPattern ;
if (pReadBuf) delete []pReadBuf ;
return retval ;
}
您是否测量了所有不同测试的完整运行时间? (包括为情况a创建1Gb文件) – 2011-01-05 12:24:57