-
-
Save kmyk/7fc8762d6ba3ce3ff402 to your computer and use it in GitHub Desktop.
私はループと配列操作を見たかったがcommon lispの入出力は遅かったため適切な比較ではなかった 指摘: https://twitter.com/nitro_idiot/status/557918405215285248 改善版: https://gist.github.com/solorab/63e63009dd070b0093e7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; $ sbcl --load a.cl --eval '(sb-ext:save-lisp-and-die "a.out" :toplevel #'\''main :executable t)' && time ( echo 100000000 | ./a.out > /dev/null ) | |
; ( echo 100000000 | ./a.out > /dev/null; ) 10.75s user 0.15s system 99% cpu 10.931 total | |
; $ sbcl --version | |
; SBCL 1.2.6 | |
(declaim (optimize (speed 3) (safety 0))) | |
(defun main () | |
(declare (type fixnum n)) | |
(declare (type (simple-array boolean (*)) is-prime)) | |
(declare (type fixnum i)) | |
(declare (type fixnum j)) | |
(setq n (the fixnum (read))) | |
(setq is-prime (make-array (1+ n) :element-type 'boolean :initial-element t)) | |
(setf (svref is-prime 0) nil) | |
(setf (svref is-prime 1) nil) | |
(dotimes (i (1+ n)) | |
(if (svref is-prime i) | |
(do ((j (* 2 i) (+ j i))) | |
((>= j (1+ n))) | |
(setf (svref is-prime j) nil)))) | |
(dotimes (i (1+ n)) | |
(when (svref is-prime i) | |
(if (not (= i 2)) (write-char #\space)) | |
(write i))) | |
(write-char #\newline)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// $ clang++ -std=c++11 -O2 a.cpp && time ( echo 100000000 | ./a.out > /dev/null ) | |
// ( echo 100000000 | ./a.out > /dev/null; ) 2.95s user 0.00s system 99% cpu 2.961 total | |
// $ g++ -std=c++11 -O2 a.cpp && time ( echo 100000000 | ./a.out > /dev/null ) | |
// ( echo 100000000 | ./a.out > /dev/null; ) 2.95s user 0.01s system 99% cpu 2.969 total | |
// $ g++ --version | |
// g++ (GCC) 4.9.2 20141224 (prerelease) | |
// $ clang++ --version | |
// clang version 3.5.1 (tags/RELEASE_351/final) | |
#include <iostream> | |
#include <vector> | |
using namespace std; | |
int main() { | |
ios_base::sync_with_stdio(false); | |
size_t n; cin >> n; | |
vector<bool> is_prime(n+1, true); | |
is_prime[0] = is_prime[1] = false; | |
for (size_t i = 0; i < n+1; ++i) { | |
if (is_prime[i]) { | |
for (size_t j = 2*i; j < n+1; j += i) { | |
is_prime[j] = false; | |
} | |
} | |
} | |
for (size_t i = 0; i < n+1; ++i) { | |
if (is_prime[i]) { | |
if (i != 2) cout << ' '; | |
cout << i; | |
} | |
} | |
cout << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
n = int(input()) | |
is_prime = [True] * (n+1) | |
is_prime[0] = is_prime[1] = False | |
for i in range(n+1): | |
if is_prime[i]: | |
j = 2*i | |
while j < n+1: | |
is_prime[j] = False | |
j += i | |
for i in range(n+1): | |
if is_prime[i]: | |
if i != 2: | |
print(' ', end='') | |
print(i, end='') | |
print() | |
# $ time ( echo 100000000 | ./a.py > /dev/null ) | |
# ( echo 100000000 | ./a.py > /dev/null; ) 207.91s user 0.14s system 99% cpu 3:28.27 total | |
# $ python3 --version | |
# Python 3.4.2 | |
# + from __future__ import print_function | |
# $ time ( echo 100000000 | python2 a.py > /dev/null ) | |
# ( echo 100000000 | python2 a.py > /dev/null; ) 159.08s user 1.48s system 99% cpu 2:40.80 total | |
# $ python2 --version | |
# Python 2.7.9 | |
# $ time ( echo 100000000 | pypy3 a.py > /dev/null ) | |
# ( echo 100000000 | pypy3 a.py > /dev/null; ) 20.35s user 0.75s system 99% cpu 21.150 total | |
# $ pypy3 --version | |
# Python 3.2.5 (b2091e973da69152b3f928bfaabd5d2347e6df46, Nov 18 2014, 20:15:54) | |
# [PyPy 2.4.0 with GCC 4.9.2] | |
# + from __future__ import print_function | |
# $ time ( echo 100000000 | pypy a.py > /dev/null ) | |
# ( echo 100000000 | pypy a.py > /dev/null; ) 19.33s user 0.55s system 99% cpu 19.928 total | |
# $ pypy --version | |
# Python 2.7.8 (c6ad44ecf5d8, Nov 18 2014, 18:04:31) | |
# [PyPy 2.4.0 with GCC 4.9.2] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; $ time ( echo 100000000 | gosh a.scm > /dev/null ) | |
; ( echo 100000000 | gosh a.scm > /dev/null; ) 35.22s user 0.29s system 101% cpu 34.964 total | |
; $ pacman -Q gauche | |
; gauche 0.9.4-1 | |
; $ time ( echo 100000000 | guile a.scm > /dev/null ) | |
; ;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0 | |
; ;;; or pass the --no-auto-compile argument to disable. | |
; ;;; compiling /home/user/Desktop/lisp/speed/a.scm | |
; ;;; compiled /home/user/.cache/guile/ccache/2.0-LE-8-2.0/home/user/Desktop/lisp/speed/a.scm.go | |
; ( echo 100000000 | guile a.scm > /dev/null; ) 103.54s user 0.17s system 99% cpu 1:43.85 total | |
; $ guile --version | |
; guile (GNU Guile) 2.0.11 | |
; $ echo 100000000 | mit-scheme --batch-mode --load a.scm | |
; ;Aborting!: out of memory | |
; ;GC #1: took: 0.30 (60%) CPU time, 0.20 (48%) real time; free: 16778766 | |
; ;GC #2: took: 0.20 (100%) CPU time, 0.20 (96%) real time; free: 16778804 | |
; $ mit-scheme --version | |
; MIT/GNU Scheme microcode 15.3 | |
; $ chicken a.scm && clang++ -I/usr/include/chicken -lchicken -O2 a.c && time ( echo 100000000 | ./a.out > /dev/null ) | |
; ( echo 100000000 | ./a.out > /dev/null; ) 21.40s user 0.42s system 99% cpu 21.873 total | |
; $ chicken --help | |
; Version 4.9.0.1 (stability/4.9.0) (rev 8b3189b) | |
; + (module a) | |
; $ bigloo -O3 -native a.scm && time ( echo 100000000 | ./a.out > /dev/null ) | |
; ( echo 100000000 | ./a.out > /dev/null; ) 10.38s user 0.09s system 99% cpu 10.495 total | |
; $ bigloo --help | |
; Bigloo (4.1a) | |
(define n (read)) | |
(define is-prime (make-vector (+ n 1) #t)) | |
(vector-set! is-prime 0 #f) | |
(vector-set! is-prime 1 #f) | |
(do ((i 0 (+ i 1))) | |
((= i (+ n 1))) | |
(if (vector-ref is-prime i) | |
(do ((j (* 2 i) (+ j i))) | |
((>= j (+ n 1))) | |
(vector-set! is-prime j #f)))) | |
(do ((i 0 (+ i 1))) | |
((= i (+ n 1))) | |
(if (vector-ref is-prime i) | |
(begin | |
(if (not (= i 2)) (write-char #\space)) | |
(display i)))) | |
(newline) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment