|
|
вернуться в форумLong reading time Functions "scanf" and "cin" in C++ read data to long. For example, 100 KB of data readed be my prog in 2-3 seconds. But on last test, there are more than 400 KB of data. And now, question: Is in C++ any function, that read data faster? Re: Long reading time Послано Megov 16 мар 2009 21:36 getchar() Re: Long reading time You're, possibly, joking =) scanf() reads data pretty fast - I rarely met problems in which you need to use faster techniques to input data. Maybe, your processing part of the program is too slow? Re: Long reading time No, I'm not joking) Even if I delete all proccesing part (leave only stream reading) all text will be readed about 2-3 seconds for 100 KB text. Or, may be, stream readed fast, but writing to console is too low? Sorry, for my English =) Re: Long reading time you are wrong #include <stdio.h> #include <stdlib.h> #include <ctime> using namespace std; int main() { int tm = clock(); FILE *f = fopen("out.txt", "w"); srand(0); int N = 100000; for (int i = 0; i < N; i++) { int x = rand(); fprintf(f, "%d\n", x); } freopen("out.txt", "r", stdin); printf("Write Time in seconds = %0.5lf\n", (clock() - tm) * 0.001); tm = clock(); int sum = 0; for (int i = 0; i < N; i++) { int x; scanf("%d", &x); sum += x; } printf("Read Time in seconds = %0.5lf\n", (clock() - tm) * 0.001); printf("%d\n", sum); return 0; } size of file is 650kb, and time is 0.125 for writing, and 0.062 for reading on my slow computer Edited by author 20.03.2009 21:18 Long reading time Alias, thanks. I'll think how to make my solution faster. :) I'm try to do same just now, and get this result: Time, needed for reading from CONSOLE (not from file) is 5.628 sec for 650 KB. Here is me prog: #include "stdio.h" #include "ctime" int main() { char c; scanf("%c", &c); int tm = clock(); while (scanf("%c", &c) != EOF) {} printf ("%0.5lf", (clock()-tm)*0.001); return 0; } Edited by author 21.03.2009 11:26 Edited by author 21.03.2009 11:26 |
|
|