c++ - Why do "printf scanf" is slower than "cout cin" in this case? -
i have tried finish uva question: 11559 - event planning. however, have found confused me. have written code using printf , scanf io, got "time limit exceed" in judge. , changed code using cin , cout, got "accepted". isn't cin or cout slower scanf , printf ? here code.
"stdio.h" version
#define inf 500000000 using namespace std; int n, b, h, w; int main () { while(scanf("%d %d %d %d", &n, &b, &h, &w)){ int cost = inf; for(int = 0; < h; i++){ int p, k ; scanf("%d",&p); for(int j = 0; j < w; j++){ scanf("%d",&k); if(k >= n && n*p < cost) cost = n*p; } } if(cost <= b) printf("%d\n",cost); else{ printf("stay home\n"); } } return 0; }
"iostream" version
#define inf 500000000 using namespace std; int n, b, h, w; int main () { while(!cin.eof()){ int cost = inf; cin >> n >> b >> h >> w; if(cin.eof()) break; for(int = 0; < h; i++){ int p, k ; cin >> p; for(int j = 0; j < w; j++){ cin >> k; if(k >= n && n*p < cost) cost = n*p; } } if(cost <= b) cout << cost << endl; else{ cout << "stay home" << endl; } } return 0; }
no, cin
, cout
aren't different using printf
/scanf
. however, endl
call ofstream::flush()
, same thing printf("%d\n", cost); fflush(stdout);
, , expect if did that, run slower there too.
also, mixing cin
scanf
or similar add more time, because code has "sync" 2 i/o streams time. suggest rewrite scanf
input as:
while(scanf("%d %d %d %d", &n, &b, &h, &w) != eof){ int cost = inf; // remove scanf , cin.eof() line here ... }
to prove point (at least output):
#include <iostream> #include <cstdio> using namespace std; static __inline__ unsigned long long rdtsc(void) { unsigned hi, lo; __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 ); } int main(int argc, char **argv) { unsigned long long t = rdtsc(); if (argc > 1) { for(int = 0; < 1000; i++) { printf("%d", i); } } else { for(int = 0; < 1000; i++) { cout << << "\n"; } } t = rdtsc() - t; cerr << "time: " << t << endl; }
the output this, when running no arguments (argc == 1
) , running argument (argc == 2
) is:
$ ./a.out > foo.txt time: 1672894 $ ./a.out 1 > foo.txt time: 1513620
approximately 10% difference in favour of printf
. however, there quite bit of variation in system when run benchmark, should taken pinch of salt. note using endl
instead of "/n"
makes significant difference!
for cin
vs. scanf
, there little more difference disadvantage of scanf
:
#include <iostream> #include <cstdio> using namespace std; static __inline__ unsigned long long rdtsc(void) { unsigned hi, lo; __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 ); } int main(int argc, char **argv) { unsigned long long t = rdtsc(); if (argc > 1) { for(int = 0; < 1000; i++) { int tmp; scanf("%d", &tmp); } } else { for(int = 0; < 1000; i++) { int tmp; cin >> tmp; } } t = rdtsc() - t; cerr << "time: " << t << endl; } $ ./a.out < foo.txt time: 1990454 $ ./a.out 1 < foo.txt time: 4804226
as can see, scanf
2.5 times slower... however, have seen other cases it's not such big difference. i'm not entirely sure why there such big difference here.
in summary, there difference, believe answer james kanze closer explaining going on - code fails complete because cin.eof()
not set scanf
.
Comments
Post a Comment