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;
}
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;
}
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;
}
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;
}
Printf Implementation: 1918ms
#include <cstdio>
using namespace std;
int main()
{
int q = 1e7;
while (q--) printf("%d", 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;
}
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)
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;
}
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;
}
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)
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;
}
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;
}
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 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));}
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');}
Printf Implementation
printf("%d", x);
synchronized(off) Cout Implementation
ios::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
synchronized(true) Cout 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: ----------
---------- Planning: ----------
- Test about random 10.000.000 big numbers
- Test with
GNU G++ 14 6.4.0
- Test with
GNU G++ 11 5.1.0
- 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)