1 / 25
Jun 2004

Please don't use stream i/o methods in C++ (ie. cin, cout) because they tend to be extremely slow.

  • created

    Jun '04
  • last reply

    Jan '18
  • 24

    replies

  • 4.3k

    views

  • 22

    users

  • 2

    likes

  • 4

    links

6 months later

i have found scanf/printf to be a lot faster than cin/cout, and getchar/putchar even faster (despite requiring extra code to parse the input)

1 month later

You're right, our tests also proved there's something wrong with (our?) g++-3.3. I think it's not exactly that they made some great improvements in 3.4: 2.95 has similar speed as 3.4.
Anyway, I was planning to switch spoj C/C++ compilers soon, probably also using stlport instead of STL from gcc (but it still needs some testing).

C++ compiler has just been upgraded to 3.4 and it really seems faster.
I was able to pass INTEST in about 15 seconds (TL is 20s); the same program ran for 66 seconds on g++-3.3.

Note: I had to use

std::ios::sync_with_stdio(false);

Without it, it was TLE even on 3.4.

1 year later

I found this thread rather interesting on this topic:

daniweb.com/techtalkforums/thread40450.html28

I found this to be the most interesting answers:

">and scanf() and printf() from their prototype know what kind of variables to expect.
And in what way do you think that the runtime type selection from a format string with scanf/printf is faster than selecting an overloaded function at compile time?"

any comments about this thread?

Nothing new, really.
We all know C++ streams have greater potential than scanf/printf (more knowledge about parameters at compilation time). Some day, compilers will learn to use it.
The compiler we use, under our conditions, has a much faster scanf/printf than cin/cout. That's all.

2 years later

This thread, while very interesting to me, is pretty old now.

What's the news on this for late 2008?

-Oscarian

10 months later

How about fread() ?

8 days later
1 year later

I am using sometimes gets()+strtok()+atoi() and puts() with sprintf()

2 years later
8 days later

If you are using cin and cout only and no printf and scanf, then you can use

ios_base::sync_with_stdio(false);

to fasten up cin and cout.

1 month later
1 month later

I almost always use printf/scanf, because they are a lot faster. When my code gets TLE and the time complexity of the solution is similar to the complexity of the input, I use getchar_unlocked() and parse the input by myself. Even scanf -- faster than cin -- is still too slow. In SPOJ BR I got 0.5s in a problem with scanf, 0.17s with getchar and 0.02s with getchar_unlocked().

It means that, if the input is O(n²) and the expected solution is O(n²), maybe you can solve it in O(n² lg n) time just by parsing the input yourself (so, you don't spend much time with the input and get a lot of extra time for the solution). PS: I don't use atoi. Converting to integer WHILE reading the input is faster... for each algarism c you read, you update your integer to: x = 10*x + (c-'0').

Wystarczy rozwiązać zagadkę matematyka. smile Proste równanie, trzeba tylko chwilę pomyśleć..

A może napisać jakieś obliczenia dla pierwszego przykładu tj. 21 6 5? Bo ciągle nie rozumiem w jaki sposób obliczyć długość ciąży?

5 years later

Dobry wieczór. Brałem pod uwagę wszystko co było napisane na forum. Ktoś pomoże?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
	int ile,x,y,z,i;
	float miesiac;
	scanf("%d",&ile);
	for(i=0;i<ile;i++){
		scanf("%d",&x);
		scanf("%d",&y);
		scanf("%d",&z);
		miesiac=x+y;
		miesiac=miesiac-z*y;
		miesiac=miesiac/(z-1);
		miesiac=miesiac*12;
		miesiac=abs(miesiac);
		miesiac=round(miesiac);
		printf("%.0f",miesiac);
	}
	return 0;
}