Here are some implementation to output numbers I found
Putchar recursive Implementation: 3525ms
#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(x);
}
int main()
{
int q = 1e7;
while (q--) writeInt(q);
return 0;
}
Putchar reverse Implementation: 3493ms
#include <iostream>
using namespace std;
#define pc putchar
inline void writeInt(int n)
{
if (n < 0) pc('-');
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;
}
Printf Implementation: 1762ms
#include <cstdio>
using namespace std;
int main()
{
int q = 1e7;
while (q--) printf("%d", q);
return 0;
}
synchronized(off) 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) 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 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;
}