{"id":125,"date":"2013-05-17T09:18:12","date_gmt":"2013-05-17T09:18:12","guid":{"rendered":"http:\/\/digital-madness.in\/blog\/?p=125"},"modified":"2018-02-17T18:01:56","modified_gmt":"2018-02-17T18:01:56","slug":"fast-io-in-c","status":"publish","type":"post","link":"http:\/\/digital-madness.in\/blog\/2013\/fast-io-in-c\/","title":{"rendered":"fast I\/O in C\/C++"},"content":{"rendered":"<p>There many ways to do input\/output in C\/C++. Some are slow, some are fast and some can be very fast. Here I&#8217;ll be discussing some of these methods.<\/p><p>Many a times during competetive programming you come across the warning &#8220;<strong>Careful &#8211; large input\/output<\/strong>&#8220;. Now what exactly does this means?<br \/>\nThis basically means that you have to use an optimized code for your reading\/writing to the standard input\/output to stay inside the <strong>tighter time constraints<\/strong> of the problem. So let&#8217;s get going and explore the different methods to optimize your I\/O in C\/C++.<\/p><p>All the code can be found here: <a href=\"https:\/\/github.com\/chirag1992m\/geeky\/tree\/master\/IO-Cplusplus\" target=\"_blank\" rel=\"noopener\">GitHub<\/a>.<\/p><p>First one is the basic method that you guys must already be using. This is based on cin\/cout methods of the iostream library. It is the most basic method and does not require you to specify the type of input you are expecting, but, is extremely slow.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;iostream&gt;\r\n#include &lt;string&gt;\r\n#include &lt;sstream&gt;\r\n\r\nusing namespace std;\r\n\r\nint main() {\r\n\t\/* integer or any integer like *\/\r\n\tint integer;\r\n\t\tcin&gt;&gt;integer;\r\n\t\tcout&lt;&lt;integer&lt;&lt;endl;\r\n\r\n\t\/* character string *\/\r\n\tchar charstring&#x5B;100];\r\n\t\tcin&gt;&gt;charstring;\t\/\/ stops input after a &quot;space&quot;\r\n\t\tcout&lt;&lt;charstring&lt;&lt;endl;\r\n\r\n\t\tcin.getline(charstring, 100);\t\/\/ stops after 100 character or eof whichever comes first\r\n\t\tcout&lt;&lt;charstring&lt;&lt;endl;\r\n\r\n\tstring strstring;\r\n\t\tcin&gt;&gt;strstring;\t\t\/\/ stops input after a &quot;space&quot;\r\n\t\tcout&lt;&lt;strstring&lt;&lt;endl;\r\n\r\n\t\tgetline(cin, strstring);\t\/\/ stops after eof or '\\n', whichever comes first\r\n\t\tcout&lt;&lt;strstring&lt;&lt;endl;\r\n\r\n\t\/* safest way to get an integer (but very slow) - taken from cpluspls.com *\/\r\n\tint number = 0;\r\n\tstring input;\r\n\twhile(true) {\r\n\t\tgetline(cin, input);\r\n\r\n\t\tstringstream myStream(input);\r\n\t\tif(myStream&gt;&gt;number)\r\n\t\t\tbreak;\r\n\t}\r\n\r\n\t\/* Safest way to get a single character *\/\r\n\tchar singlechar = {0};\r\n\twhile(true) {\r\n\t\tgetline(cin, input);\r\n\r\n\t\tif(input.length() == 1){\r\n\t\t\tsinglechar = input&#x5B;0];\r\n\t\t\tbreak;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre><p>The second method is based on the simple printf\/scanf functions of stdio.h library for C. These are much faster than the above method and can be easily used in competetive programming for a decent time limit. These are multi-thread safe as they lock the file before writing.<br \/>\nDetailed description of the stdio.h library you can refer to <a href=\"http:\/\/www.codecogs.com\/reference\/computing\/c\/stdio.h\/\">here<\/a>.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;cstdio&gt;\r\n#include &lt;string&gt;\r\n\r\nint main() {\r\n\t\/* integer I\/O *\/\r\n\tint a;\r\n\tscanf(&quot;%d&quot;, &a);\r\n\tprintf(&quot;%d\\n&quot;, a);\r\n\r\n\t\/*\r\n        Other format specifiers.\r\n        %d, %i = signed integer\r\n        %u = unsigned integer\r\n        %l = prefix for long\r\n        %f = signed floating point\r\n        %e = signed scientific\r\n        %c = single character\r\n\t*\/\r\n\r\n\t\/* sting I\/O *\/\r\n\tchar charstring&#x5B;100];\r\n\tscanf(&quot;%s&quot;, charstring);    \/\/ only till the first white space is stored\r\n\tprintf(&quot;%s\\n&quot;, charstring);\r\n\r\n\tscanf(&quot;%&#x5B;^\\n]s&quot;, charstring);   \/\/ sets th delimeter to be &quot;new line&quot;\r\n\tprintf(&quot;%s\\n&quot;, charstring); \/\/ thus whole line is read until a \\n is observed\r\n\t\t\t\t\t\t\t\t\/\/ does not eliminate \\n from the input stream\r\n\r\n\tgets(charstring);\r\n\tprintf(&quot;%s\\n&quot;, charstring);\r\n}\r\n<\/pre><p>This method is very fast than the last method and used <strong>unlocked <\/strong>versions of the above functions used. I have written the code to scan integer and a string. For others you can easily write your function referring the below code. These are NOT multi-thread safe. Should be used in caution. But, in competetive programming plateforms, one program is already separated from others, so can easilt be used for better I\/O in competetive programming which have even tighter time constraints.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;cstdio&gt;\r\n\r\ninline void fastRead_int(int &x) {\r\n    register int c = getchar_unlocked();\r\n    x = 0;\r\n    int neg = 0;\r\n\r\n    for(; ((c&lt;48 || c&gt;57) && c != '-'); c = getchar_unlocked());\r\n\r\n    if(c=='-') {\r\n    \tneg = 1;\r\n    \tc = getchar_unlocked();\r\n    }\r\n\r\n    for(; c&gt;47 && c&lt;58 ; c = getchar_unlocked()) {\r\n    \tx = (x&lt;&lt;1) + (x&lt;&lt;3) + c - 48;\r\n    }\r\n\r\n    if(neg)\r\n    \tx = -x;\r\n}\r\n\r\ninline void fastRead_string(char *str)\r\n{\r\n    register char c = 0;\r\n    register int i = 0;\r\n\r\n    while (c &lt; 33)\r\n        c = getchar_unlocked();\r\n\r\n    while (c != '\\n') {\r\n        str&#x5B;i] = c;\r\n        c = getchar_unlocked();\r\n        i = i + 1;\r\n    }\r\n\r\n    str&#x5B;i] = '&#92;&#48;';\r\n}\r\n\r\nint main()\r\n{\r\n\r\n  int n;\r\n  char s&#x5B;100];\r\n\r\n  fastRead_int(n);\r\n  \tprintf(&quot;%d\\n&quot;, n);\r\n\r\n  fastRead_string(s);\r\n  \tprintf(&quot;%s\\n&quot;, s);\r\n  return 0;\r\n}\r\n<\/pre><p>There is another method I came across a solution in one of the problems on <a href=\"http:\/\/www.codechef.com\/\">codechef<\/a>. I haven&#8217;t tested it&#8217;s performance, but, his solution had the lowest timing with the same logic for the actual problem. So, I am assuming this gives a better performance for I\/O. You can download the code <a href=\"https:\/\/github.com\/chirag1992m\/geeky\/blob\/master\/IO-Cplusplus\/method4.cpp\">here<\/a>.<br \/>\nThe solution referrenced is <a href=\"http:\/\/www.codechef.com\/viewsolution\/244848\">this<\/a>.<\/p><p>I&#8217;ll come up with more updates on the same topic.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There many ways to do input\/output in C\/C++. Some are slow, some are fast and some can be very fast. Here I&#8217;ll be discussing some of these methods.Many a times during competetive programming you come across the warning &#8220;Careful &#8211; large input\/output&#8220;. Now what exactly does this means? This basically means that you have to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"","footnotes":""},"categories":[38,39],"tags":[13,33,34,35],"class_list":["post-125","post","type-post","status-publish","format-standard","hentry","category-cpp","category-time","tag-c","tag-fast-io","tag-input","tag-output"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/digital-madness.in\/blog\/wp-json\/wp\/v2\/posts\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/digital-madness.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/digital-madness.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/digital-madness.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/digital-madness.in\/blog\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":10,"href":"http:\/\/digital-madness.in\/blog\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"predecessor-version":[{"id":200,"href":"http:\/\/digital-madness.in\/blog\/wp-json\/wp\/v2\/posts\/125\/revisions\/200"}],"wp:attachment":[{"href":"http:\/\/digital-madness.in\/blog\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/digital-madness.in\/blog\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/digital-madness.in\/blog\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}