I'm programming in C++ and I've been told that "printf" is faster than "cout", so since then I've been using "printf" instead of "cout".

Do you recommend my option or not?

thanks

I'd recommend reading this first:
[thread]40450[/thread]

<<split from that old thread referenced above that is now closed>>

#include <iostream>
#include <cstdio>
#include <ctime>

int main()
{
  std::clock_t start1, start2;
  double diff1, diff2;

  start1 = std::clock();
  for ( int i = 0; i < 100000; i++ )
    std::cout<<"a";
  diff1 = ( std::clock() - start1 ) / (double)CLOCKS_PER_SEC;

  start2 = std::clock();
  for ( int i = 0; i < 100000; i++ )
    printf ( "a" );
  diff2 = ( std::clock() - start2 ) / (double)CLOCKS_PER_SEC;
http://www.daniweb.com/techtalkforums/dani-images/smilies/fiyellow/icon_mrgreen.gif
:mrgreen:
  std::cout<<"cout:   "<< diff1 <<'\n'
    <<"printf: "<< diff2 <<'\n';
}

Redefinition of variable "i"
:twisted:

my quick test using VC++ 2005 Pro shows that cout is faster than printf

#include <cstdio>
#include <ctime>
#include <fstream>
#include <iostream>
using namespace std;

int foo1()
{
    clock_t t1,t2;
    t1 = clock();
    for(int i = 0; i < 100000; i++)
        printf("Hello World\n");
    t2 = clock();
    return t2 - t1;
}
int foo2()
{
    clock_t t1,t2;
    t1 = clock();
    for(int i = 0; i < 100000; i++)
        cout << "Hello World\n";
    t2 = clock();
    return t2 - t1;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int d1,d2;
    d1 = foo1();
    d2 = foo2();
    cout << "\n\n\n" << d2 - d1 << endl;
	return 0;
}

final time output was 33873 on my computer. So whether one is faster than the other depends on the compiler, operating system, bus speed, and several other factors.

are you using VC++ 6.0 compiler? Yes, then the problem is because that compiler pre-dates current c++ standards. Update to VC++ 2005 express and the code you posted will compile correctly.

>Redefinition of variable "i"
Get a smarter compiler. Some older, lamer compilers (*cough* Visual Studio 6 *cough*) don't properly implement the scope rules for a variable defined in the initialization clause of a for loop.

my quick test using VC++ 2005 Pro shows that cout is faster than printf
final time output was 33873 on my computer. So whether one is faster than the other depends on the compiler, operating system, bus speed, and several other factors.

I've tested and my result was the opposite, thanks for the hand ;)

>Redefinition of variable "i"
Get a smarter compiler. Some older, lamer compilers (*cough* Visual Studio 6 *cough*) don't properly implement the scope rules for a variable defined in the initialization clause of a for loop.

I can´t have a new one.
I'm working in investigation at the university and this is the one that they gave me to install...

I've tested and my result was the opposite, thanks for the hand ;)

See what I mean -- there are lots of 'if's. You can't just make a blanket statement that printf() is faster than cout.

mods: how did this thread get trashed? it has two different topics???

I can´t have a new one.
I'm working in investigation at the university and this is the one that they gave me to install...

well, in that case change the code. declare variable i at the top of the function and remove the declarations from each loop. Its not really all that hard to do.

If you can't change the code, then you are SOL :mrgreen:

>I can´t have a new one.
Then ask how to use a workaround if you can't figure it out. The impression I got from your post was not that you were getting an error and wanted to know how to fix it, but that you were trying to sound smart by incorrectly correcting perfectly valid code. Though it could have been the smiley you used.

mods: how did this thread get trashed? it has two different topics???

Look up.

<<split from that old thread referenced above that is now closed>>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.