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

Общий форум

Here is a way to accelerate your I/O speed (using C)
Послано tiancaihb 11 авг 2010 17:50
Before, the scanf/printf function works very fast and cin/cout has a reasonable speed. However, both of them are extremely slow nowadays, with MSVC2010 compiler. I used putchar() and getchar() to realise I/O, they used to be even faster than stdio. But they are slower than stdio now.
I have found out that the main cause is that MS's stdio.h is different from GCC's one. I don't understand header files much, but have worked out a way to make it swifter. Here it is:

First, copy the codes of "getchar" and "putchar" from your GCC's stdio.h. (If you don't have one, you may copy from below) To avoid coincide with original function, you have to rename them, eg. "getchar1" and "putchar1".
Then, for example, Prob.1196, you have to write two functions: int getInt() and void printInt(int num), whose functions are shown by their names. (Below I use ms and mp)
In the functions, you mustn't use scanf or printf. You ought to use putchar and getchar.
Below is mine crude version of them:

///////////////////////////////////////////////
inline int __cdecl getchar1 (void)
{
  return (--stdin->_cnt >= 0)
    ?  (int) (unsigned char) *stdin->_ptr++
    : _filbuf (stdin);
}
inline int __cdecl putchar1(int __c)
{
  return (--stdout->_cnt >= 0)
    ?  (int) (unsigned char) (*stdout->_ptr++ = (char)__c)
    :  _flsbuf (__c, stdout);}
int ms(){
int s=0;
char c;
while(1){
c=getchar1();
if(c>='0'&&c<='9')break;
}
s=c-'0';
while(1){
c=getchar1();
if(c<'0'||c>'9')break;
s=s*10+c-'0';
}
return s;
}
void mp(int s){
     char b[12];
     int l=0,i,m=0;
     if(s==0){
       putchar1(48);
       return;
     }
     if(s<0){
       m=1;
       s=-s;
     }
     while(s){
       b[l]=s%10;
       l++;
       s/=10;
     }
     if(m)putchar1('-');
     for(i=l-1;i>=0;i--)putchar1(b[i]+'0');
}
///////////////////////////////////////////////

How to use? For instance, A+B Prob:
////////////////////////
int main(){
int a,b;
a=ms();
b=ms();
mp(a+b);
}
/////////////////////////

Not very beautiful coding style though......

How sufficient they are? Let's see the following statistc:
Problem 1196 with almost same code:

Old Compiler with ms() and mp(): 2780220 19:02:10 5 Oct 2009 tiancaihb 1196 C++ Accepted
 0.062 1 625 KB

New Compiler with ms() and mp(): 3128057 17:26:43 11 Aug 2010 tiancaihb 1196 C++ Accepted
 0.093 1 604 KB

New Compiler with scanf() and printf(): 3128068 17:32:18 11 Aug 2010 tiancaihb 1196 C++ Accepted
 0.296 1 616 KB

New Compiler with cin and cout: 3128071 17:33:31 11 Aug 2010 tiancaihb 1196 C++ Accepted
 1.218 1 700 KB



Sorry for my bad English. Hope this will help some of you!
:)

Edited by author 11.08.2010 17:55

Edited by author 11.08.2010 17:55
Re: Here is a way to accelerate your I/O speed (using C)
Послано Andrew Hoffmann aka SKYDOS [Vladimir SU] 11 авг 2010 17:55
And if somebody needs faster ways to read data in C# and using less memory here is my data-read code: http://paste.ubuntu.com/476401/
Re: Here is a way to accelerate your I/O speed (using C)
Послано tiancaihb 11 авг 2010 18:20
Isn't ther sth like Console.write() you can use? (I don't know about C#)
Re: Here is a way to accelerate your I/O speed (using C)
Послано Evgeniy++ 12 авг 2010 12:51
Interesting! :-)

You think it is worth the job? Using non-standart input/output functions may lead to errors, while giving only 100-200 ms difference.
Re: Here is a way to accelerate your I/O speed (using C)
Послано tiancaihb 12 авг 2010 14:24
Of course! It's very exciting climbing atop the Rating list!