ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1037. Управление памятью

Long reading time
Послано Ildar Valiev 16 мар 2009 20:49
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
Послано Vedernikoff Sergey (HSE: АОП) 16 мар 2009 21:39
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
Послано Ildar Valiev 20 мар 2009 10:48
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
Послано Alias aka Alexander Prudaev 20 мар 2009 21:13
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
Послано Ildar Valiev 20 мар 2009 23:58
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