----------
Purpose:
- First, sorry for my bad English.
- Second, I know there are lots of topics about this. But after solving a problem that need FastIO, I feel interested in FastIO and decide to do the implementations and comparing them. But it maybe time-consuming so I make this post for ones who maybe try to find the Effeciency-Simple-IO, so you can save time instead of searching more.
- Third, usually it is need not to use FastIO. In the contest, they usually have twice or more time of the solutions code than the given time limitation. Dont worry about faster IO if it is not needed, you should improve your algorithms first, maybe you can use Bitwise Operations for x8 x32 x64 faster.
- Fourth, if the problem need to be solve in O(n) ~ O(n log n), and your algorithms work in O(n ^ 2), this Micro-Optimization doesnt help you at all (maybe you will get some more points but hard get AC), you should change the algorithms for further approach.
- Final, for some problems that needed to be solved by FastIO, you can use the suitable template and modify it for your solving the problem. Dont use the template if you dont really know how to use (I may add a guide path to the post in the future)
- Conclusion, this post is about comparing the implementations for each version of C++ language and you should choose the most suitable for you.
Here are some implementations to output numbers I found
Time to write first 10.000.000 non-negative numbers
(base on Codeforces Custom Test // GNU G++ 17 7.3.0 // GNU G++ 14 6.4.0 // GNU G++ 11 5.1.0)
Sort by GNU G++ 17 7.3.0
Putchar Non-recursive Dividing Implementation: 3649ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10)
putchar(x / p + '0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Putchar Recursive Implementation: 3510ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
putchar(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
writeRec(x);
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Putchar Reverse Implementation: 3462ms
#include <iostream>
using namespace std;
#define pc putchar
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Putchar Non-recursive toString Implementation: 3369ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) putchar('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) putchar(os[i] + 48);
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Printf Implementation: 1762ms
#include <cstdio>
using namespace std;
int main()
{
int q = 1e7;
while (q--) printf("%d", q);
return 0;
}
synchronized(off) Cout Implementation: 1356ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
int q = 1e7;
while (q--) cout << q;
return 0;
}
synchronized(true) Cout Implementation: 1060ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(true),cin.tie(NULL),cout.tie(NULL);
int q = 1e7;
while (q--) cout << q;
return 0;
}
Normal Cout Implementation: 1045ms
#include <iostream>
using namespace std;
int main()
{
int q = 1e7;
while (q--) cout << q;
return 0;
}
Unlocked-Putchar Non-recursive Dividing Implementation: 919ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) _putchar_nolock('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10)
_putchar_nolock(x / p + '0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Unlocked-Putchar Reverse Implementation: 842ms
#include <iostream>
using namespace std;
#define pc _putchar_nolock
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Unlocked-Putchar Recursive Implementation: 701ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
_putchar_nolock(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) _putchar_nolock('-'), x = -x;
writeRec(x);
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Unlocked-Putchar Non-recursive toString Implementation: 670ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) _putchar_nolock('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) _putchar_nolock(os[i] + 48);
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
fwrite buffer Implementation: 545ms
#include <iostream>
using namespace std;
static const int buf_len = (1 << 14);
static const int buf_max = (1 << 04);
static char buf_out[buf_len];
static char buf_num[buf_max];
static int buf_pos = 0;
inline void writeChar(int x) {
if (buf_pos == buf_len) fwrite(buf_out, 1, buf_len, stdout), buf_pos = 0;
buf_out[buf_pos++] = x;
}
inline void writeInt(int x, char end = 0) {
if (x < 0) writeChar('-'), x = -x;
int n = 0;
do buf_num[n++] = x % 10 + '0'; while(x /= 10);
while (n--) writeChar(buf_num[n]);
if (end) writeChar(end);
}
struct Flusher{~Flusher(){if(buf_pos)fwrite(buf_out, 1, buf_pos, stdout),buf_pos=0;}}flusher;
int main()
{
int q = 1e7;
while (q--) writeInt(q); Flusher();
return 0;
}
Sort by GNU G++ 14 6.4.0
Putchar Non-recursive Dividing Implementation: 3525ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10)
putchar(x / p + '0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Putchar Reverse Implementation: 3447ms
#include <iostream>
using namespace std;
#define pc putchar
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Putchar Recursive Implementation: 3354ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
putchar(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
writeRec(x);
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Putchar Non-recursive toString Implementation: 3353ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) putchar('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) putchar(os[i] + 48);
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Printf Implementation: 1669ms
#include <cstdio>
using namespace std;
int main()
{
int q = 1e7;
while (q--) printf("%d", q);
return 0;
}
synchronized(off) Cout Implementation: 1356ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
int q = 1e7;
while (q--) cout << q;
return 0;
}
synchronized(true) Cout Implementation: 1091ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(true),cin.tie(NULL),cout.tie(NULL);
int q = 1e7;
while (q--) cout << q;
return 0;
}
Normal Cout Implementation: 1075ms
#include <iostream>
using namespace std;
int main()
{
int q = 1e7;
while (q--) cout << q;
return 0;
}
Unlocked-Putchar Non-recursive Dividing Implementation: 888ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) _putchar_nolock('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10)
_putchar_nolock(x / p + '0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Unlocked-Putchar Reverse Implementation: 685ms
#include <iostream>
using namespace std;
#define pc _putchar_nolock
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Unlocked-Putchar Recursive Implementation: 561ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
_putchar_nolock(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) _putchar_nolock('-'), x = -x;
writeRec(x);
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Unlocked-Putchar Non-recursive toString Implementation: 546ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) _putchar_nolock('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) _putchar_nolock(os[i] + 48);
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
fwrite buffer Implementation: 498ms
#include <iostream>
using namespace std;
static const int buf_len = (1 << 14);
static const int buf_max = (1 << 04);
static char buf_out[buf_len];
static char buf_num[buf_max];
static int buf_pos = 0;
inline void writeChar(int x) {
if (buf_pos == buf_len) fwrite(buf_out, 1, buf_len, stdout), buf_pos = 0;
buf_out[buf_pos++] = x;
}
inline void writeInt(int x, char end = 0) {
if (x < 0) writeChar('-'), x = -x;
int n = 0;
do buf_num[n++] = x % 10 + '0'; while(x /= 10);
while (n--) writeChar(buf_num[n]);
if (end) writeChar(end);
}
struct Flusher{~Flusher(){if(buf_pos)fwrite(buf_out, 1, buf_pos, stdout),buf_pos=0;}}flusher;
int main()
{
int q = 1e7;
while (q--) writeInt(q); Flusher();
return 0;
}
Sort by GNU G++ 11 5.1.0
synchronized(off) Cout Implementation: 2901ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
int q = 1e7;
while (q--) cout << q;
return 0;
}
synchronized(true) Cout Implementation: 2901ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(true),cin.tie(NULL),cout.tie(NULL);
int q = 1e7;
while (q--) cout << q;
return 0;
}
Normal Cout Implementation: 2900ms
#include <iostream>
using namespace std;
int main()
{
int q = 1e7;
while (q--) cout << q;
return 0;
}
Printf Implementation: 1918ms
#include <cstdio>
using namespace std;
int main()
{
int q = 1e7;
while (q--) printf("%d", q);
return 0;
}
Putchar Non-recursive Dividing Implementation: 795ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10)
putchar(x / p + '0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Putchar Reverse Implementation: 592ms
#include <iostream>
using namespace std;
#define pc putchar
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Putchar Non-recursive toString Implementation: 451ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) putchar('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) putchar(os[i] + 48);
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Putchar Recursive Implementation: 389ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
putchar(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
writeRec(x);
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
fwrite buffer Implementation: 405ms
#include <iostream>
using namespace std;
static const int buf_len = (1 << 14);
static const int buf_max = (1 << 04);
static char buf_out[buf_len];
static char buf_num[buf_max];
static int buf_pos = 0;
inline void writeChar(int x) {
if (buf_pos == buf_len) fwrite(buf_out, 1, buf_len, stdout), buf_pos = 0;
buf_out[buf_pos++] = x;
}
inline void writeInt(int x, char end = 0) {
if (x < 0) writeChar('-'), x = -x;
int n = 0;
do buf_num[n++] = x % 10 + '0'; while(x /= 10);
while (n--) writeChar(buf_num[n]);
if (end) writeChar(end);
}
struct Flusher{~Flusher(){if(buf_pos)fwrite(buf_out, 1, buf_pos, stdout),buf_pos=0;}}flusher;
int main()
{
int q = 1e7;
while (q--) writeInt(q); Flusher();
return 0;
}
Sort by Microsoft Visual C++ 2010
synchronized(true) Cout Implementation: 4477ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(true),cin.tie(NULL),cout.tie(NULL);
int q = 1e7;
while (q--) cout << q;
return 0;
}
synchronized(off) Cout Implementation: 4414ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
int q = 1e7;
while (q--) cout << q;
return 0;
}
Normal Cout Implementation: 4351ms
#include <iostream>
using namespace std;
int main()
{
int q = 1e7;
while (q--) cout << q;
return 0;
}
Putchar Reverse Implementation: 2667ms
#include <iostream>
using namespace std;
#define pc putchar
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Putchar Non-recursive Dividing Implementation: 2666ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10)
putchar(x / p + '0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Putchar Non-recursive toString Implementation: 2635ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) putchar('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) putchar(os[i] + 48);
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Putchar Recursive Implementation: 2620ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
putchar(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
writeRec(x);
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Printf Implementation: 2058ms
#include <cstdio>
using namespace std;
int main()
{
int q = 1e7;
while (q--) printf("%d", q);
return 0;
}
fwrite buffer Implementation: 358ms
#include <iostream>
using namespace std;
static const int buf_len = (1 << 14);
static const int buf_max = (1 << 04);
static char buf_out[buf_len];
static char buf_num[buf_max];
static int buf_pos = 0;
inline void writeChar(int x) {
if (buf_pos == buf_len) fwrite(buf_out, 1, buf_len, stdout), buf_pos = 0;
buf_out[buf_pos++] = x;
}
inline void writeInt(int x, char end = 0) {
if (x < 0) writeChar('-'), x = -x;
int n = 0;
do buf_num[n++] = x % 10 + '0'; while(x /= 10);
while (n--) writeChar(buf_num[n]);
if (end) writeChar(end);
}
struct Flusher{~Flusher(){if(buf_pos)fwrite(buf_out, 1, buf_pos, stdout),buf_pos=0;}}flusher;
int main()
{
int q = 1e7;
while (q--) writeInt(q); Flusher();
return 0;
}
Sort by Microsoft Visual C++ 2017
synchronized(off) Cout Implementation: 5241ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
int q = 1e7;
while (q--) cout << q;
return 0;
}
synchronized(true) Cout Implementation: 5210ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(true),cin.tie(NULL),cout.tie(NULL);
int q = 1e7;
while (q--) cout << q;
return 0;
}
Normal Cout Implementation: 5054ms
#include <iostream>
using namespace std;
int main()
{
int q = 1e7;
while (q--) cout << q;
return 0;
}
Putchar Non-recursive Dividing Implementation: 2558ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10)
putchar(x / p + '0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Putchar Reverse Implementation: 2557ms
#include <iostream>
using namespace std;
#define pc putchar
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Putchar Non-recursive toString Implementation: 2542ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) putchar('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) putchar(os[i] + 48);
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Putchar Recursive Implementation: 2527ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
putchar(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
writeRec(x);
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Printf Implementation: 2230ms
#include <cstdio>
using namespace std;
int main()
{
int q = 1e7;
while (q--) printf("%d", q);
return 0;
}
fwrite buffer Implementation: 311ms
#include <iostream>
using namespace std;
static const int buf_len = (1 << 14);
static const int buf_max = (1 << 04);
static char buf_out[buf_len];
static char buf_num[buf_max];
static int buf_pos = 0;
inline void writeChar(int x) {
if (buf_pos == buf_len) fwrite(buf_out, 1, buf_len, stdout), buf_pos = 0;
buf_out[buf_pos++] = x;
}
inline void writeInt(int x, char end = 0) {
if (x < 0) writeChar('-'), x = -x;
int n = 0;
do buf_num[n++] = x % 10 + '0'; while(x /= 10);
while (n--) writeChar(buf_num[n]);
if (end) writeChar(end);
}
struct Flusher{~Flusher(){if(buf_pos)fwrite(buf_out, 1, buf_pos, stdout),buf_pos=0;}}flusher;
int main()
{
int q = 1e7;
while (q--) writeInt(q); Flusher();
return 0;
}
Time to write 1.000 times first 10000 numbers
(base on Codeforces Custom Test // GNU G++ 17 7.3.0 // GNU G++ 14 6.4.0 // GNU G++ 11 5.1.0)
Sort by GNU G++ 17 7.3.0
Putchar Reverse Implementation: 1981ms
#include <iostream>
using namespace std;
#define pc putchar
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Putchar Non-recursive Dividing Implementation: 1980ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10)
putchar(x / p + '0');
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Putchar Non-recursive toString Implementation: 1934ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) putchar('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) putchar(os[i] + 48);
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Putchar Recursive Implementation: 1887ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
putchar(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
writeRec(x);
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Printf Implementation: 1216ms
#include <cstdio>
using namespace std;
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
printf("%d", i);
return 0;
}
synchronized(on) Implementation: 873ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(true),cin.tie(NULL),cout.tie(NULL);
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
cout << i;
return 0;
}
Normal Cout Implementation: 873ms
#include <iostream>
using namespace std;
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
cout << i;
return 0;
}
synchronized(off) Cout Implementation: 857ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
cout << i;
return 0;
}
Unlocked-Putchar Non-recursive Dividing Implementation: 483ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) _putchar_nolock('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10)
_putchar_nolock(x / p + '0');
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Unlocked-Putchar Reverse Implementation: 467ms
#include <iostream>
using namespace std;
#define pc _putchar_nolock
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Unlocked-Putchar Recursive Implementation: 358ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
_putchar_nolock(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) _putchar_nolock('-'), x = -x;
writeRec(x);
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Unlocked-Putchar Non-recursive toString Implementation: 373ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) _putchar_nolock('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) _putchar_nolock(os[i] + 48);
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
fwrite buffer Implementation: 311ms
#include <iostream>
using namespace std;
static const int buf_len = (1 << 14);
static const int buf_max = (1 << 04);
static char buf_out[buf_len];
static char buf_num[buf_max];
static int buf_pos = 0;
inline void writeChar(int x) {
if (buf_pos == buf_len) fwrite(buf_out, 1, buf_len, stdout), buf_pos = 0;
buf_out[buf_pos++] = x;
}
inline void writeInt(int x, char end = 0) {
if (x < 0) writeChar('-'), x = -x;
int n = 0;
do buf_num[n++] = x % 10 + '0'; while(x /= 10);
while (n--) writeChar(buf_num[n]);
if (end) writeChar(end);
}
struct Flusher{~Flusher(){if(buf_pos)fwrite(buf_out, 1, buf_pos, stdout),buf_pos=0;}}flusher;
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
Flusher();
return 0;
}
Sort by GNU G++ 14 6.4.0
Putchar Reverse Implementation: 1996ms
#include <iostream>
using namespace std;
#define pc putchar
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Putchar Non-recursive Dividing Implementation: 1996ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10)
putchar(x / p + '0');
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Putchar Non-recursive toString Implementation: 1903ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) putchar('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) putchar(os[i] + 48);
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Putchar Recursive Implementation: 1871ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
putchar(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
writeRec(x);
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Printf Implementation: 1153ms
#include <cstdio>
using namespace std;
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
printf("%d", i);
return 0;
}
synchronized(off) Cout Implementation: 920ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
cout << i;
return 0;
}
synchronized(on) Implementation: 888ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(true),cin.tie(NULL),cout.tie(NULL);
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
cout << i;
return 0;
}
Normal Cout Implementation: 873ms
#include <iostream>
using namespace std;
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
cout << i;
return 0;
}
Unlocked-Putchar Non-recursive Dividing Implementation: 452ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) _putchar_nolock('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10)
_putchar_nolock(x / p + '0');
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Unlocked-Putchar Reverse Implementation: 404ms
#include <iostream>
using namespace std;
#define pc _putchar_nolock
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Unlocked-Putchar Non-recursive toString Implementation: 296ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) _putchar_nolock('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) _putchar_nolock(os[i] + 48);
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Unlocked-Putchar Recursive Implementation: 295ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
_putchar_nolock(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) _putchar_nolock('-'), x = -x;
writeRec(x);
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
fwrite buffer Implementation: 233ms
#include <iostream>
using namespace std;
static const int buf_len = (1 << 14);
static const int buf_max = (1 << 04);
static char buf_out[buf_len];
static char buf_num[buf_max];
static int buf_pos = 0;
inline void writeChar(int x) {
if (buf_pos == buf_len) fwrite(buf_out, 1, buf_len, stdout), buf_pos = 0;
buf_out[buf_pos++] = x;
}
inline void writeInt(int x, char end = 0) {
if (x < 0) writeChar('-'), x = -x;
int n = 0;
do buf_num[n++] = x % 10 + '0'; while(x /= 10);
while (n--) writeChar(buf_num[n]);
if (end) writeChar(end);
}
struct Flusher{~Flusher(){if(buf_pos)fwrite(buf_out, 1, buf_pos, stdout),buf_pos=0;}}flusher;
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
Flusher();
return 0;
}
Sort by GNU G++ 11 5.1.0
synchronized(on) Implementation: 2698ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(true),cin.tie(NULL),cout.tie(NULL);
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
cout << i;
return 0;
}
Normal Cout Implementation: 2698ms
#include <iostream>
using namespace std;
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
cout << i;
return 0;
}
synchronized(off) Cout Implementation: 2620ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
cout << i;
return 0;
}
Printf Implementation: 1403ms
#include <cstdio>
using namespace std;
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
printf("%d", i);
return 0;
}
Putchar Non-recursive Dividing Implementation: 405ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10)
putchar(x / p + '0');
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Putchar Reverse Implementation: 343ms
#include <iostream>
using namespace std;
#define pc putchar
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
fwrite buffer Implementation: 265ms
#include <iostream>
using namespace std;
static const int buf_len = (1 << 14);
static const int buf_max = (1 << 04);
static char buf_out[buf_len];
static char buf_num[buf_max];
static int buf_pos = 0;
inline void writeChar(int x) {
if (buf_pos == buf_len) fwrite(buf_out, 1, buf_len, stdout), buf_pos = 0;
buf_out[buf_pos++] = x;
}
inline void writeInt(int x, char end = 0) {
if (x < 0) writeChar('-'), x = -x;
int n = 0;
do buf_num[n++] = x % 10 + '0'; while(x /= 10);
while (n--) writeChar(buf_num[n]);
if (end) writeChar(end);
}
struct Flusher{~Flusher(){if(buf_pos)fwrite(buf_out, 1, buf_pos, stdout),buf_pos=0;}}flusher;
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
Flusher();
return 0;
}
Putchar Non-recursive toString Implementation: 249ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) putchar('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) putchar(os[i] + 48);
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Putchar Recursive Implementation: 233ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
putchar(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
writeRec(x);
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Sort by Microsoft Visual C++ 2010
Normal Cout Implementation: 3962ms
#include <iostream>
using namespace std;
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
cout << i;
return 0;
}
synchronized(on) Implementation: 3915ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(true),cin.tie(NULL),cout.tie(NULL);
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
cout << i;
return 0;
}
synchronized(off) Cout Implementation: 3884ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
cout << i;
return 0;
}
Printf Implementation: 1715ms
#include <cstdio>
using namespace std;
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
printf("%d", i);
return 0;
}
Putchar Reverse Implementation: 1559ms
#include <iostream>
using namespace std;
#define pc putchar
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Putchar Non-recursive Dividing Implementation: 1544ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10)
putchar(x / p + '0');
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Putchar Recursive Implementation: 1513ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
putchar(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
writeRec(x);
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Putchar Non-recursive toString Implementation: 1512ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) putchar('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) putchar(os[i] + 48);
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
fwrite buffer Implementation: 202ms
#include <iostream>
using namespace std;
static const int buf_len = (1 << 14);
static const int buf_max = (1 << 04);
static char buf_out[buf_len];
static char buf_num[buf_max];
static int buf_pos = 0;
inline void writeChar(int x) {
if (buf_pos == buf_len) fwrite(buf_out, 1, buf_len, stdout), buf_pos = 0;
buf_out[buf_pos++] = x;
}
inline void writeInt(int x, char end = 0) {
if (x < 0) writeChar('-'), x = -x;
int n = 0;
do buf_num[n++] = x % 10 + '0'; while(x /= 10);
while (n--) writeChar(buf_num[n]);
if (end) writeChar(end);
}
struct Flusher{~Flusher(){if(buf_pos)fwrite(buf_out, 1, buf_pos, stdout),buf_pos=0;}}flusher;
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
Flusher();
return 0;
}
Sort by Microsoft Visual C++ 2017
synchronized(on) Implementation: 4772ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(true),cin.tie(NULL),cout.tie(NULL);
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
cout << i;
return 0;
}
synchronized(off) Cout Implementation: 4710ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
cout << i;
return 0;
}
Normal Cout Implementation: 4695ms
#include <iostream>
using namespace std;
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
cout << i;
return 0;
}
Printf Implementation: 1747ms
#include <cstdio>
using namespace std;
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
printf("%d", i);
return 0;
}
Putchar Non-recursive Dividing Implementation: 1435ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10)
putchar(x / p + '0');
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Putchar Reverse Implementation: 1435ms
#include <iostream>
using namespace std;
#define pc putchar
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Putchar Non-recursive toString Implementation: 1419ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) putchar('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) putchar(os[i] + 48);
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
Putchar Recursive Implementation: 1419ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
putchar(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
writeRec(x);
}
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
return 0;
}
fwrite buffer Implementation: 171ms
#include <iostream>
using namespace std;
static const int buf_len = (1 << 14);
static const int buf_max = (1 << 04);
static char buf_out[buf_len];
static char buf_num[buf_max];
static int buf_pos = 0;
inline void writeChar(int x) {
if (buf_pos == buf_len) fwrite(buf_out, 1, buf_len, stdout), buf_pos = 0;
buf_out[buf_pos++] = x;
}
inline void writeInt(int x, char end = 0) {
if (x < 0) writeChar('-'), x = -x;
int n = 0;
do buf_num[n++] = x % 10 + '0'; while(x /= 10);
while (n--) writeChar(buf_num[n]);
if (end) writeChar(end);
}
struct Flusher{~Flusher(){if(buf_pos)fwrite(buf_out, 1, buf_pos, stdout),buf_pos=0;}}flusher;
int main()
{
int q = 1e3;
while (q--)
for (int i = 0; i <= 1e4; ++i)
writeInt(i);
Flusher();
return 0;
}
Time to write first 10.000.000 non-positive numbers
(base on Codeforces Custom Test // GNU G++ 17 7.3.0 // GNU G++ 14 6.4.0 // GNU G++ 11 5.1.0)
Sort by GNU G++ 17 7.3.0
Putchar Non-recursive toString Implementation: 3946ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) putchar('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) putchar(os[i] + 48);
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Putchar Reverse Implementation: 3930ms
#include <iostream>
using namespace std;
#define pc putchar
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Putchar Recursive Implementation: 3915ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
putchar(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) putchar('-');
writeRec(abs(x));
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Putchar Non-recursive Dividing Implementation: 3883ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10) putchar(x / p + '0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Printf Implementation: 1808ms
#include <cstdio>
using namespace std;
int main()
{
int q = 1e7;
while (q--) printf("%d", -q);
return 0;
}
synchronized(off) Cout Implementation: 1481ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
int q = 1e7;
while (q--) cout << -q;
return 0;
}
synchronized(true) Cout Implementation: 1092ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(true),cin.tie(NULL),cout.tie(NULL);
int q = 1e7;
while (q--) cout << -q;
return 0;
}
Normal Cout Implementation: 1092ms
#include <iostream>
using namespace std;
int main()
{
int q = 1e7;
while (q--) cout << -q;
return 0;
}
Unlocked-Putchar Non-recursive Dividing Implementation: 1045ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) _putchar_nolock('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10)
_putchar_nolock(x / p + '0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Unlocked-Putchar Reverse Implementation: 857ms
#include <iostream>
using namespace std;
#define pc _putchar_nolock
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Unlocked-Putchar Non-recursive toString Implementation: 795ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) _putchar_nolock('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) _putchar_nolock(os[i] + 48);
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Unlocked-Putchar Recursive Implementation: 779ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
_putchar_nolock(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) _putchar_nolock('-'), x = -x;
writeRec(x);
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
fwrite buffer Implementation: 561ms
#include <iostream>
using namespace std;
static const int buf_len = (1 << 14);
static const int buf_max = (1 << 04);
static char buf_out[buf_len];
static char buf_num[buf_max];
static int buf_pos = 0;
inline void writeChar(int x) {
if (buf_pos == buf_len) fwrite(buf_out, 1, buf_len, stdout), buf_pos = 0;
buf_out[buf_pos++] = x;
}
inline void writeInt(int x, char end = 0) {
if (x < 0) writeChar('-'), x = -x;
int n = 0;
do buf_num[n++] = x % 10 + '0'; while(x /= 10);
while (n--) writeChar(buf_num[n]);
if (end) writeChar(end);
}
struct Flusher{~Flusher(){if(buf_pos)fwrite(buf_out, 1, buf_pos, stdout),buf_pos=0;}}flusher;
int main()
{
int q = 1e7;
while (q--) writeInt(-q); Flusher();
return 0;
}
Sort by GNU G++ 14 6.4.0
Putchar Reverse Implementation: 3899ms
#include <iostream>
using namespace std;
#define pc putchar
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Putchar Recursive Implementation: 3899ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
putchar(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) putchar('-');
writeRec(abs(x));
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Putchar Non-recursive Dividing Implementation: 3883ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10) putchar(x / p + '0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Putchar Non-recursive toString Implementation: 3860ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) putchar('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) putchar(os[i] + 48);
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Printf Implementation: 1762ms
#include <cstdio>
using namespace std;
int main()
{
int q = 1e7;
while (q--) printf("%d", -q);
return 0;
}
synchronized(off) Cout Implementation: 1512ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
int q = 1e7;
while (q--) cout << -q;
return 0;
}
Normal Cout Implementation: 1153ms
#include <iostream>
using namespace std;
int main()
{
int q = 1e7;
while (q--) cout << -q;
return 0;
}
synchronized(true) Cout Implementation: 1092ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(true),cin.tie(NULL),cout.tie(NULL);
int q = 1e7;
while (q--) cout << -q;
return 0;
}
Unlocked-Putchar Non-recursive Dividing Implementation: 951ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) _putchar_nolock('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10)
_putchar_nolock(x / p + '0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Unlocked-Putchar Reverse Implementation: 732ms
#include <iostream>
using namespace std;
#define pc _putchar_nolock
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Unlocked-Putchar Recursive Implementation: 655ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
_putchar_nolock(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) _putchar_nolock('-'), x = -x;
writeRec(x);
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Unlocked-Putchar Non-recursive toString Implementation: 638ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) _putchar_nolock('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) _putchar_nolock(os[i] + 48);
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
fwrite buffer Implementation: 529ms
#include <iostream>
using namespace std;
static const int buf_len = (1 << 14);
static const int buf_max = (1 << 04);
static char buf_out[buf_len];
static char buf_num[buf_max];
static int buf_pos = 0;
inline void writeChar(int x) {
if (buf_pos == buf_len) fwrite(buf_out, 1, buf_len, stdout), buf_pos = 0;
buf_out[buf_pos++] = x;
}
inline void writeInt(int x, char end = 0) {
if (x < 0) writeChar('-'), x = -x;
int n = 0;
do buf_num[n++] = x % 10 + '0'; while(x /= 10);
while (n--) writeChar(buf_num[n]);
if (end) writeChar(end);
}
struct Flusher{~Flusher(){if(buf_pos)fwrite(buf_out, 1, buf_pos, stdout),buf_pos=0;}}flusher;
int main()
{
int q = 1e7;
while (q--) writeInt(-q); Flusher();
return 0;
}
Sort by GNU G++ 11 5.1.0
synchronized(true) Cout Implementation: 2901ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(true),cin.tie(NULL),cout.tie(NULL);
int q = 1e7;
while (q--) cout << -q;
return 0;
}
Normal Cout Implementation: 2901ms
#include <iostream>
using namespace std;
int main()
{
int q = 1e7;
while (q--) cout << -q;
return 0;
}
synchronized(off) Cout Implementation: 2885ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
int q = 1e7;
while (q--) cout << -q;
return 0;
}
Printf Implementation: 1950ms
#include <cstdio>
using namespace std;
int main()
{
int q = 1e7;
while (q--) printf("%d", -q);
return 0;
}
Putchar Non-recursive Dividing Implementation: 810ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10) putchar(x / p + '0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Putchar Reverse Implementation: 624ms
#include <iostream>
using namespace std;
#define pc putchar
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
fwrite buffer Implementation: 483ms
#include <iostream>
using namespace std;
static const int buf_len = (1 << 14);
static const int buf_max = (1 << 04);
static char buf_out[buf_len];
static char buf_num[buf_max];
static int buf_pos = 0;
inline void writeChar(int x) {
if (buf_pos == buf_len) fwrite(buf_out, 1, buf_len, stdout), buf_pos = 0;
buf_out[buf_pos++] = x;
}
inline void writeInt(int x, char end = 0) {
if (x < 0) writeChar('-'), x = -x;
int n = 0;
do buf_num[n++] = x % 10 + '0'; while(x /= 10);
while (n--) writeChar(buf_num[n]);
if (end) writeChar(end);
}
struct Flusher{~Flusher(){if(buf_pos)fwrite(buf_out, 1, buf_pos, stdout),buf_pos=0;}}flusher;
int main()
{
int q = 1e7;
while (q--) writeInt(-q); Flusher();
return 0;
}
Putchar Non-recursive toString Implementation: 483ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) putchar('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) putchar(os[i] + 48);
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Putchar Recursive Implementation: 452ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
putchar(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) putchar('-');
writeRec(abs(x));
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Sort by Microsoft Visual C++ 2010
synchronized(true) Cout Implementation: 4508ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(true),cin.tie(NULL),cout.tie(NULL);
int q = 1e7;
while (q--) cout << -q;
return 0;
}
Normal Cout Implementation: 4461ms
#include <iostream>
using namespace std;
int main()
{
int q = 1e7;
while (q--) cout << -q;
return 0;
}
synchronized(off) Cout Implementation: 4461ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
int q = 1e7;
while (q--) cout << -q;
return 0;
}
Putchar Reverse Implementation: 3057ms
#include <iostream>
using namespace std;
#define pc putchar
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Putchar Recursive Implementation: 3025ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
putchar(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) putchar('-');
writeRec(abs(x));
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Putchar Non-recursive toString Implementation: 2995ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) putchar('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) putchar(os[i] + 48);
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Putchar Non-recursive Dividing Implementation: 2994ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10) putchar(x / p + '0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Printf Implementation: 2090ms
#include <cstdio>
using namespace std;
int main()
{
int q = 1e7;
while (q--) printf("%d", -q);
return 0;
}
fwrite buffer Implementation: 374ms
#include <iostream>
using namespace std;
static const int buf_len = (1 << 14);
static const int buf_max = (1 << 04);
static char buf_out[buf_len];
static char buf_num[buf_max];
static int buf_pos = 0;
inline void writeChar(int x) {
if (buf_pos == buf_len) fwrite(buf_out, 1, buf_len, stdout), buf_pos = 0;
buf_out[buf_pos++] = x;
}
inline void writeInt(int x, char end = 0) {
if (x < 0) writeChar('-'), x = -x;
int n = 0;
do buf_num[n++] = x % 10 + '0'; while(x /= 10);
while (n--) writeChar(buf_num[n]);
if (end) writeChar(end);
}
struct Flusher{~Flusher(){if(buf_pos)fwrite(buf_out, 1, buf_pos, stdout),buf_pos=0;}}flusher;
int main()
{
int q = 1e7;
while (q--) writeInt(-q); Flusher();
return 0;
}
Sort by Microsoft Visual C++ 2017
synchronized(true) Cout Implementation: 5225ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(true),cin.tie(NULL),cout.tie(NULL);
int q = 1e7;
while (q--) cout << -q;
return 0;
}
synchronized(off) Cout Implementation: 5163ms
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
int q = 1e7;
while (q--) cout << -q;
return 0;
}
Normal Cout Implementation: 5148ms
#include <iostream>
using namespace std;
int main()
{
int q = 1e7;
while (q--) cout << -q;
return 0;
}
Putchar Reverse Implementation: 2932ms
#include <iostream>
using namespace std;
#define pc putchar
inline void writeInt(int n)
{
if (n < 0) pc('-'), n = -n;
if (!n) return (void)pc('0');
int t = n, cnt = 0, rev = 0;
while (!(t%10)) { cnt++; t /= 10;}
do {rev=rev*10 + n%10;} while(n /= 10);
do {pc(rev % 10 + 48);} while(rev /= 10);
while (cnt--) pc('0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Putchar Non-recursive Dividing Implementation: 2885ms
#include <iostream>
using namespace std;
inline void writeInt(int x)
{
if (x < 0) putchar('-'), x = -x;
int p = 1;
for (int t = x / 10; t > 0; t /= 10)
p *= 10;
for (; p > 0; x %= p, p /= 10) putchar(x / p + '0');
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Putchar Recursive Implementation: 2870ms
#include <iostream>
using namespace std;
inline void writeRec(int n) {
if (n > 9) writeRec(n / 10);
putchar(char(n % 10 + '0'));
}
inline void writeInt(int x)
{
if (x < 0) putchar('-');
writeRec(abs(x));
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Putchar Non-recursive toString Implementation: 2807ms
#include <iostream>
using namespace std;
char os[20];
inline void writeInt(int n)
{
if (n < 0) putchar('-'), n = -n;
int i=0;
do os[i++] = n%10; while(n/=10);
while (i--) putchar(os[i] + 48);
}
int main()
{
int q = 1e7;
while (q--) writeInt(-q);
return 0;
}
Printf Implementation: 2245ms
#include <cstdio>
using namespace std;
int main()
{
int q = 1e7;
while (q--) printf("%d", -q);
return 0;
}
fwrite buffer Implementation: 342ms
#include <iostream>
using namespace std;
static const int buf_len = (1 << 14);
static const int buf_max = (1 << 04);
static char buf_out[buf_len];
static char buf_num[buf_max];
static int buf_pos = 0;
inline void writeChar(int x) {
if (buf_pos == buf_len) fwrite(buf_out, 1, buf_len, stdout), buf_pos = 0;
buf_out[buf_pos++] = x;
}
inline void writeInt(int x, char end = 0) {
if (x < 0) writeChar('-'), x = -x;
int n = 0;
do buf_num[n++] = x % 10 + '0'; while(x /= 10);
while (n--) writeChar(buf_num[n]);
if (end) writeChar(end);
}
struct Flusher{~Flusher(){if(buf_pos)fwrite(buf_out, 1, buf_pos, stdout),buf_pos=0;}}flusher;
int main()
{
int q = 1e7;
while (q--) writeInt(-q); Flusher();
return 0;
}
Single Line Template
Putchar Non-recursive toString Implementation
char os[20];
inline void writeInt(int n){if(n<0)putchar('-'),n=-n;int i=0;do{os[i++]=n%10;}while(n/=10);while(i--)putchar(os[i]+48);}
Putchar Non-recursive Dividing Implementation
inline void writeInt(int x){if(x<0)putchar('-'),x=-x;int p=1;for(int t=x/10;t>0;t/=10)p*=10;for(;p>0;x%=p,p/=10)putchar(x/p+'0');}
Putchar Reverse Implementation
#define pc putchar
inline void writeInt(int n){if(n<0)pc('-'),n=-n;if(!n)return(void)pc('0');int t=n,cnt=0,rev=0;while(!(t%10)){cnt++;t/=10;}do{rev=rev*10+n%10;}while(n/=10);do{pc(rev%10+48);}while(rev/=10);while(cnt--)pc('0');}
Putchar Recursive Implementation
inline void writeRec(int n){if(n>9)writeRec(n/10);putchar(char(n%10+'0'));}
inline void writeInt(int x){if(x<0)putchar('-');writeRec(abs(x));}
Unlocked-Putchar Non-recursive toString Implementation
char os[20];
inline void writeInt(int n){if(n<0)_putchar_nolock('-'),n=-n;int i=0;do{os[i++]=n%10;}while(n/=10);while(i--)_putchar_nolock(os[i]+48);}
Unlocked-Putchar Non-recursive Dividing Implementation
inline void writeInt(int x){if(x<0)_putchar_nolock('-'),x=-x;int p=1;for(int t=x/10;t>0;t/=10)p*=10;for(;p>0;x%=p,p/=10)_putchar_nolock(x/p+'0');}
Unlocked-Putchar Reverse Implementation
#define pc _putchar_nolock
inline void writeInt(int n){if(n<0)pc('-'),n=-n;if(!n)return(void)pc('0');int t=n,cnt=0,rev=0;while(!(t%10)){cnt++;t/=10;}do{rev=rev*10+n%10;}while(n/=10);do{pc(rev%10+48);}while(rev/=10);while(cnt--)pc('0');}
Unlocked-Putchar Recursive Implementation
inline void writeRec(int n){if(n>9)writeRec(n/10);_putchar_nolock(char(n%10+'0'));}
inline void writeInt(int x){if(x<0)_putchar_nolock('-');writeRec(abs(x));}
Printf Implementation
printf("%d", x);
synchronized(off) Implementation
ios::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
synchronized(true) Implementation
ios::sync_with_stdio(true),cin.tie(NULL),cout.tie(NULL);
Cout Implementation
cout << x;
fwrite buffer Implementation
static const int buf_len = (1 << 14), buf_max = (1 << 04);
static char buf_out[buf_len], buf_num[buf_max];
static int buf_pos = 0;
inline void writeChar(int x){if(buf_pos==buf_len)fwrite(buf_out,1,buf_len,stdout),buf_pos=0;buf_out[buf_pos++]=x;}
inline void writeInt(int x,char end=0){if(x<0)writeChar('-'),x=-x;int n=0;do{buf_num[n++]=x%10+'0';}while(x/=10);while(n--)writeChar(buf_num[n]);if(end)writeChar(end);}
struct Flusher{~Flusher(){if(buf_pos)fwrite(buf_out, 1, buf_pos, stdout),buf_pos=0;}}flusher;
----------
About:
- putchar()
- putchar_unlocked()
- putchar_nolock()
- printf()
- sync_with_stdio()
- cin.tie(), cout.tie()
- cout <<
- fwrite
----------
Similiar Topic:
- C++ Input Implementations ///// Are there ways to perform faster ?
- Fast IO c++
- Fast I/O Code Optimizer
- Yet again on C++ input/output
- How to take fast I/O in C++?
- Fast and furious C++ I/O
- Best form of C++ I/O?
- What is the fastest way to take I/O in c++?
- Fast I/O using fread, fwrite
- Speed of Input/Output of various languages and method on Codeforces
- Speed up cin & cout in C++
----------
Planning:
- Test about random 10.000.000 big numbers
- Test about 10.000.000 numbers 2 ^ k, k integer
- More implementations (Actually the post is about Faster and Faster Short Output Implementation but I will add some for speed comparing)
- New output types (long long, double, string)
Post's Source is too big to put into the spoiler :'(
Post's origin
Add "to write [1.000 times first 10000 numbers]"
Add "to write [first 10.000.000 non-positive numbers]"
Add Single Line Template
Current
Past: Add and sort
Unlocked-Putchar Implementations
Curr: Start working on
Input Implementations
Next: Test with
Microsoft Visual C++
let me tell you about c++11:
Oh, wow, I thought
C++ 17
is always faster ? This is new to me, thanks <3I will test
C++ 11
andC++ 14
later after the contest. Thanks for your suggestionBy the way, do you know where can I input large amount of code and count the time to run the program ? I want to test
Input Implementations
tooYou can test input and output there ($$$10^6$$$ numbers). My input/output works in
280 ms
.I want to test somewhere
Just Input
, since another factors may results wrong inputingexcution time
(I only want to test with input speed only). Is there a website for testing like that ?[Deleted]
If you are interested, I have an implementation of fast input and output classes on github. Link.
Behavior is same as
std::cin
orstd::cout
in most competitive programming cases:operator<<
andoperator>>
can be used, there issetprecision
for real numbers. There is examples of using there.Code works in
638 ms
for C++17 and498 ms
for C++11.Maybe it can be improved for runtime.
Thanks and Yes I am interested <3 But for now I just want to test some implementations which are simple (easy to code and debug or modify) but efficiency to output
does it mean that cout is faster than printf, isn't it?
I will add the bench mark later
Replacing the
putchar
function with_putchar_nolock
which avoids thread-locking overhead in GNU G++17 as follows reduced the execution time of your "Putchar Non-recursive Dividing Implementation" code using Codeforces Custom Test from 3649ms to less than 850ms.Oh, I read somewhere that cant use
putchar_unlocked()
am going to test thoseThe function
_putchar_nolock
is not availble in GNU G++11 5.1.0. The fastest performance of your code with this function (561 msec) is achieved using Microsoft Visual C++ 2010._putchar_nolock is slower than putchar in c++11 but still better option on c++17
Which C++11 compiler, header file and namespace did you use? I got the following error message when I used the GNU G++11 5.1.0 compiler in Codeforces Custom Test with
Invocation failed [COMPILATION_ERROR]
Can't compile file:
program.cpp: In function 'void write_int_aux(int)':
program.cpp:7:29: error: '_putchar_nolock' was not declared in this scope
_putchar_nolock is not a thing on c++11
just saying that _putchar_nolock on c++17 is slower than putchar on c++11