Constant optimization that little thing

Source: Internet
Author: User

(This article is Ben by the code of the Gods.)

(If you can point out the principle or fallacy, please leave a message)

Test environment:

Os:linux Ubuntu 14.04LTS

Memory:8gb

PROCESSOR:INTEL®CORE™I7-4500U CPU @ 1.80ghzx4

Compiler:gnu g++ 4.8.4 (Switch ON-O3 optimization in release mode)

Part I: input (if there is no special description, the input type is int,scanf only one parameter is received per execution)

C + + iostream optimization statement (to avoid mixing C + + io and C-style io after use):

Std::ios::sync_with_stdio (0);

Std::cin.tie (0);

Std::cin is approximately 4 times times more time-consuming than scanf when not optimized (whether or not the-ox compilation optimization is turned on)

Combined with optimized time-consuming and scanf equivalent

Read-in optimization when not open-ox and scanf compared with no advantage, open-ox after scanf faster 10%~15%

And the speed of the scanf seems to be related to the number of parameters received at a time, the input amount, one read 5 times than one read 1 fast 10%~15%, and is not affected by the compilation optimization switch

1#include <iostream>2#include <cctype>3#include <initializer_list>4 5 #if__cplusplus < 201103L6#include <bits/c++0x_warning.h>7 #endif //__cplusplus8 9Template <classIstreamtype,classInttype>Ten void_read_positive_integer (istreamtype& ist, inttype&dest) One { ADest =0; -     CharCH = ist.Get(); -      while(!isdigit (ch)) ch = ist.Get(); the      while(IsDigit (CH)) -     { -Dest *=Ten; -Dest + = CH-'0'; +CH = ist.Get(); -     } + } A  atTemplate <classIstreamtype,classInttype> - void_read_integer (istreamtype& ist, inttype&dest) - { -Dest =0; -     CharCH = ist.Get(); -      while(!isdigit (CH) && ch! ='-') ch = ist.Get(); in     if(ch = ='-') -     { toCH = ist.Get(); +          while(IsDigit (CH)) -         { theDest *=Ten; *Dest-= CH-'0'; $CH = ist.Get();Panax Notoginseng         } -     } the     Else  while(IsDigit (CH)) +         { ADest *=Ten; theDest + = CH-'0'; +CH = ist.Get(); -         } $ } $  -Template <classIstreamtype,classInttype,class... Args> -InlinevoidReadpositiveinteger (istreamtype& ist, inttype&First , theargs&.. args) - {Wuyistd::initializer_list<int> dummy = the{(_read_positive_integer (ist, first),0), (_read_positive_integer (ist, args),0)... }; - } Wu  -Template <classIstreamtype,classInttype,class... Args> AboutInlinevoidReadinteger (istreamtype& ist, inttype& First, args&.. args) $ { -std::initializer_list<int> dummy = -{(_read_integer (ist, first),0), (_read_integer (ist, args),0)... }; -}
input Optimization with C + + style input

———— to Be Continued ————

Constant optimization that little thing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.