Skip to content

Instantly share code, notes, and snippets.

@fukamachi
Forked from kmyk/a.cl
Created January 21, 2015 15:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save fukamachi/8cba8a26f5e1c0dc8609 to your computer and use it in GitHub Desktop.
Save fukamachi/8cba8a26f5e1c0dc8609 to your computer and use it in GitHub Desktop.
; $ 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
(defun main ()
(declare (optimize (speed 3) (debug 0) (safety 0) (compilation-speed 0)))
(let* ((n (1+ (the fixnum (read))))
(is-prime (make-array n :element-type 'boolean :initial-element t)))
(declare (type fixnum n)
(type (simple-array boolean (*)) is-prime))
(setf (svref is-prime 0) nil)
(setf (svref is-prime 1) nil)
(dotimes (i n)
(declare (type fixnum i))
(if (svref is-prime i)
(do ((j (* 2 i) (+ j i)))
((>= j n))
(declare (type fixnum j))
(setf (svref is-prime j) nil))))
(loop for i from 0 to (1- n)
when (svref is-prime i)
collect i)))
// $ 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;
}
#!/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]
; $ 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