how to speed up stdout?


hi, any one knows what i need to do to make this code work faster

for(j=0;j<1000000;j++){
printf("%d \t",j);
}

its taking 15,023s.
my goal is to make it in 13s, can u help me?
Faster processor? Not displaying some/all numbers?
Why do you need it to be faster. Will you be able to read 1000000 numbers in 13 seconds?
Change j++ to ++j
@LB Seriously? Do care that much about such a tiny thing that makes no difference whatsoever.
It makes a massive difference in debug mode. With optimizations it's the same.
^ then compile with optimizations.
Last edited on
I'm just following the rule of thumb that says you never use post increment or post decrement unless you have a valid reason to use the 'old' return value.
closed account (zb0S216C)
I agree with LB on this. That 1 extra parameter puts me off. But some scenarios, however, require its use.

Wazzak
well... You could use _itoa and print (not printf!).
By doing something similar with file pointers and a 7+mb file, i sped up a loop from ~25s to ~10s. Considering my hd is slow as hell...
Remember that printf calls vsprintf, but print doesn't.
$ whatis _itoa print
_itoa: nothing appropriate.
print: nothing appropriate.
Ahem? He looped through int-only conversion... He only needed _itoa, not a %d/parameter... And even if he did just _itoa, printf will keep on checking for %'s.
I think ne555 is trying to say that these are non-standard functions that joaoaug may not have.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    static char buffer[1024*1024*4] ;
    // implementation defined : this may or may not have an effect
    // an implementation may choose to do nothing about the buffer
    std::cout.rdbuf()->pubsetbuf( buffer, sizeof(buffer) ) ;

    // this should speed it up
    std::cout.sync_with_stdio(false) ;

    for( int j=0; j<100000 ; ++j ) std::cout << j << ' ' ;

    // C equivalent of the above:
    // static char buffer[1024*1024*4] ;
    // int j = 0 ;
    // setvbuf( stdout, buffer, _IOFBF, sizeof(buffer) ) ;
    // for( ; j<100000 ; ++j ) fprintf( stdout, "%d ", j ) ; 
Last edited on
Topic archived. No new replies allowed.