void perfprint(unsigned int count)
{
char a[100] = "fosjkdfjlsjdflw0304802";
for(unsigned int i = 0;i<count;++i)
{
printf("%s", a);
}
}
void perfcout(unsigned int count)
{
char a[100] = "fosjkdfjlsjdflw0304802";
for(unsigned int i = 0;i<count;++i)
{
cout << a;
}
}
Environment : C++, VS 2010, Windows 7, 32-bit, Core-i7, 4GB, 3.40 GHz
I tested both the functions with count = 10000
for 5
times each.
Measured the performance using QueryPerformanceCounter
.
perfprint
> ~850
milliseconds (Avg of 5 runs)
perfcout
> ~9000
milliseconds (Avg of 5 runs)
Does this mean printf
is ~10x
faster than cout
?
Edit:
With /Ox, /Ot, No debug information in Release build
and with std::ios_base::sync_with_stdio(false);
in perfcout
method,
result is same for cout
i.e. ~9000 millisecs
Edit 2:
To conclude, cout
is faster than printf
. The reason of the observations above were due to console output. When redirecting output to file, things turned on its head!