14 / 25
Aug 2013

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;
}

1) jeżeli dane są całkowite i wynik ma by całkowity, to obliczenia wykonujemy na liczbach całkowitych (powtarzam to już po raz tysięczny chyba smile)

2) wzór jest zły (niedużo[color=#FF4000], trzeba trochę poprawic, gdy liczymy na liczbach całkowitych[/color])

3) czy oglądałeś wyście programu dla danych testowych - z przekierowanym wejściem z pliku, lub np. na http://ideone.com8 ?

2 years later

Please stop using archaic code for the wrong reasons.

// never use endl instead of '\n' since endl forces a useless but expensive flush
#define endl '\n'

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); // NULL if you're old fashioned.
}

Voila! cin/cout is faster than scanf/printf. But if you really need speed I/O, cin.read() and parse it yourself.

7 months later

Hi; I am trying to use std::cin.readsome() for very fast input. Unfortunately the solution is working on my terminal but not on some judging servers. The same solutions transated to fread() from cstdio worked perfectly…

namespace FI {
 const int L = 1 << 15 | 1;
 char buf[L], *front, *back;
 void nextChar(char &);
 template <typename T>void nextNumber(T &);
}

void FI::nextChar(char &c) {
 if(front == back) std::cin.readsome(buf, L), back = (front=buf) + std::cin.gcount();
 c = !std::cin.gcount() ? (char)EOF : *front++;
}

template<typename T>void FI::nextNumber(T &x) {
 char c; int f = 1;
 for(nextChar(c); c>'9'||c<'0'; nextChar(c)) if(c == '-') f = -1;
 for(x=0; c>='0'&& c<='9'; nextChar(c)) x = x*10+c-'0';
 x *= f;
}
2 months later

It is possible to make cin/cout almost as faster as scanf/printf by using this
ios_base::sync_with_stdio(false);cin.tie(NULL);
Using ‘\n’ instead of endl also makes output faster but beware of the differences between ‘\n’ and endl;these are not exactly similar,they do have differences.

The stream’s buffering will improve performance by reducing the number of OS write calls. You could increase the stream’s buffer size to improve performance.