简体   繁体   English

快速输入输出功能

[英]Fast input output function

#define getcx getchar_unlocked
inline void inp( int &n )//fast input function
{
   n=0;
   int ch=getcx();int sign=1;
   while( ch < '0' || ch > '9' ){if(ch=='-')sign=-1; ch=getcx();}

   while(  ch >= '0' && ch <= '9' )
           n = (n<<3)+(n<<1) + ch-'0', ch=getcx();
   n=n*sign;
}

Hi I have been using the above function for input in various coding contests but was never able to understand why is it fast. 嗨我一直在各种编码竞赛中使用上述功能输入,但从来没有能够理解为什么它很快。 I know the logic but don't know the concept of it's fastness. 我知道逻辑,但不知道它的坚牢度的概念。 For example what is this line doing "#define getcx getchar_unlocked" . 例如,这行是做什么的“#define getcx getchar_unlocked”。 Also I don't know any fast output function so is there any fast output function also 另外我不知道任何快速输出功能,所以还有任何快速输出功能

The #define uses the preprocessor to make getcx be a short-hand for the function getchar_unlocked() , which is a non-locking character-reading function. #define使用预处理器使getcx成为函数getchar_unlocked() ,这是一个非锁定字符读取函数。

It's a bit awesome that you've competed in several coding contests without understanding this pretty basic piece of C. 你在参加几个编码竞赛时没有理解这个非常基本的C片,这有点太棒了。

The manual page I linked to above mentions putc_unlocked() which sounds like pretty much the same thing but for output. 我上面链接的手册页提到了putc_unlocked() ,这听起来几乎和输出一样。

getchar_unlocked() is the thread unsafe version of getchar() The reason that getchar_unlocked() seems faster is that it doesn't check for any locks on the input stream from where it is supposed to fetch a character. getchar_unlocked()getchar()的线程不安全版本getchar_unlocked() 似乎更快的原因是它不检查输入流上应该从哪里获取字符的任何锁定。 So if another thread has locked the input stream, this thread is supposed to wait till lock count has come to zero . 因此,如果另一个线程已锁定输入流,则该线程应该等到锁定计数变为零 But this function doesn't care about it, thereby destroying synchronisation between threads. 但是这个函数并不关心它,从而破坏了线程之间的同步。

But if you are sure that lack of synchronisation wont harm you, then this function might help you to be a bit faster. 但如果您确定缺少同步不会伤害您,那么此功能可能会帮助您更快一点。

Also, its advised that you can use it safely only when the invoking thread has locked stdin using flockfile() (or ftrylockfile() ). 此外,它建议您只有在调用线程使用flockfile() (或ftrylockfile() )锁定stdin时才能安全地使用它。

Define a macro called getcx such that no locks will be used while reading. 定义一个名为getcx的宏,以便在读取时不使用锁。 This is not thread safe but faster if you are not worried about thread safety: 这不是线程安全的,但如果您不担心线程安全性会更快:

#define getcx getchar_unlocked

Define inp as inline so that it is faster: inp定义为内联,以便更快:

inline void inp( int &n )//fast input function 
{
   n=0;
   int ch=getcx();int sign=1;
   while( ch < '0' || ch > '9' ){if(ch=='-')sign=-1; ch=getcx();}

Multiply n by 10 (using shift to compute 8*n+2*n, which might be faster): 将n乘以10(使用shift计算8 * n + 2 * n,这可能更快):

   while(  ch >= '0' && ch <= '9' )
           n = (n<<3)+(n<<1) + ch-'0', ch=getcx();
   n=n*sign;
}

You can use putchar_unlocked to have a faster output function when thread safety is not an issue. 当线程安全性不是问题时,您可以使用putchar_unlocked来获得更快的输出函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM