출력 속도 비교

여러가지 언어와 출력 방법을 이용해서 시간이 얼마나 걸리는지 비교해 보았습니다.

방법: 총 N개의 줄에 1부터 10,000,000까지의 자연수를 한 줄에 하나씩 출력하는 시간을 측정. 10번 측정해서 평균값으로 순위를 매김

순위 언어 출력 방법 평균 (초)
1 C11 fwrite 0.4423
2 C++17 ios_base::sync_with_stdio(false); cout << i << '\n'; 0.827
3 C++17 ios_base::sync_with_stdio(false); cout.tie(NULL); cout << i << '\n'; 0.8272
4 C++17 printf("%d\n",i); 0.8614
5 C11 printf("%d\n",i); 0.9118
6 C++17 cout << i << '\n'; 0.9229
7 Java BufferedWriter, bf.write(i + "\n"); 0.9581
8 PyPy for i in xrange(1,n+1): sys.stdout.write(str(i)+'\n') 0.9847
9 C++17 s += to_string(i) + '\n';를 이용해 문자열 하나로 만든 다음, 마지막에 cout << s << '\n'; 1.1507
10 Java StringBuilder를 이용해 문자열 하나로 만든 다음, System.out.println(sb); 1.1881
11 Java BufferedWriter, bf.write(Integer.toString(i)); bf.newLine(); 1.2556
12 PyPy3 for i in range(1,n+1): sys.stdout.write(str(i)+'\n') 1.3722
13 PyPy print '\n'.join(map(str,xrange(1,n+1))) 1.3738
14 PyPy sys.stdout.write('\n'.join(map(str,xrange(1,n+1)))) 1.3772
15 PyPy for i in xrange(1,n+1): print i 1.4968
16 Python 2 print '\n'.join(map(str,xrange(1,n+1))) 1.7621
17 Python 2 sys.stdout.write('\n'.join(map(str,xrange(1,n+1)))) 1.7658
18 Java PrintWriter 1.954
19 Python 3 print('\n'.join(map(str,range(1,n+1)))) 2.3312
20 Python 3 sys.stdout.write('\n'.join(map(str,range(1,n+1)))) 2.337
21 PyPy sys.stdout.write(''.join(str(i)+'\n' for i in xrange(1,n+1))) 2.3935
22 PyPy print ''.join(str(i)+'\n' for i in xrange(1,n+1)) 2.3974
23 Python 2 sys.stdout.write(''.join(str(i)+'\n' for i in xrange(1,n+1))) 2.536
24 Python 2 print ''.join(str(i)+'\n' for i in xrange(1,n+1)) 2.5372
25 PyPy3 for i in range(1,n+1): print(i) 3.051
26 Python 2 for i in xrange(1,n+1): print i 3.069
27 C# 6.0 StreamWriter 3.0959
28 PyPy3 sys.stdout.write('\n'.join(map(str,range(1,n+1)))) 3.5625
29 PyPy3 print('\n'.join(map(str,range(1,n+1)))) 3.566
30 Python 3 sys.stdout.write(''.join(str(i)+'\n' for i in range(1,n+1))) 3.6766
31 Python 3 print(''.join(str(i)+'\n' for i in range(1,n+1))) 3.6836
32 PyPy3 print(''.join(str(i)+'\n' for i in range(1,n+1))) 3.8326
33 PyPy3 sys.stdout.write(''.join(str(i)+'\n' for i in range(1,n+1))) 3.8339
34 C# 6.0 StringBuilder를 이용해 문자열 하나로 만든 다음, Console.Write(sb); 3.8562
35 Python 2 for i in xrange(1,n+1): sys.stdout.write(str(i)+'\n') 4.3475
36 Python 3 for i in range(1,n+1): sys.stdout.write(str(i)+'\n') 5.3699
37 Python 3 for i in range(1,n+1): print(i) 5.8186
38 PyPy for i in xrange(1,n+1): os.write(1,str(i)+'\n') 10.4553
39 C++17 cout << i << endl; 11.5322
40 PyPy3 for i in range(1,n+1): os.write(1,(str(i)+'\n').encode('utf-8')) 12.0509
41 Python 2 for i in xrange(1,n+1): os.write(1,str(i)+'\n') 14.8269
42 Python 3 for i in range(1,n+1): os.write(1,(str(i)+'\n').encode('utf-8')) 18.2189
43 Java System.out.println(i); 30.013
44 C# 6.0 Console.WriteLine(i); 30.1438

언어 정보

  • C11
    • gcc (GCC) 7.2.0
    • gcc a.c -o bin/a -O2 -std=c99
  • C++17
    • g++ (GCC) 7.2.0
    • g++ a.cpp -o bin/a -O2 -std=c++17
  • Java
    • javac 1.8.0_151
    • javac Main.java
  • Python 2
    • Python 2.7.14
    • python a.py
  • PyPy
    • PyPy 5.10.0 with GCC 6.2.0 20160901
    • pypy a.py
  • Python 3
    • Python 3.6.3
    • python3 a.py
  • PyPy3
    • PyPy 5.10.0 with GCC 6.2.0 20160901
    • pypy3 a.py
  • C# 6.0
    • Mono C# compiler version 4.2.1.0
    • mcs -codepage:utf8 -warn:0 -optimize+ -checked+ -clscheck- -reference:System.Numerics.dll Main.cs
    • mono --optimize=all Main.exe

댓글 (3개) 댓글 쓰기


solarmagic 3주 전

cout.tie(NULL); 을 안하는게 더 빠르네요


baekjoon 2주 전

0.0002초의 차이는 빠른게 아니고 그냥 영향이 없는게 아닐까요


yclock 4일 전

C11에서 fwrite를 쓸 때, int를 string으로 어떻게 변환하셨나요?