Browse Source

Move prime stuff to tsoding/prime-benchmark repo

rexim 5 years ago
parent
commit
ab06d4c9ff
6 changed files with 2 additions and 130 deletions
  1. 1 7
      Makefile
  2. 1 27
      README.md
  3. 0 19
      prime-naive.js
  4. 0 6
      prime-wasm.js
  5. 0 31
      prime.c
  6. 0 40
      prime.wat

+ 1 - 7
Makefile

@@ -1,13 +1,7 @@
 BNGVIEWER_CFLAGS=$(shell pkg-config --cflags sdl2)
 BNGVIEWER_CFLAGS=$(shell pkg-config --cflags sdl2)
 BNGVIEWER_LIBS=$(shell pkg-config --libs sdl2)
 BNGVIEWER_LIBS=$(shell pkg-config --libs sdl2)
 
 
-all: prime prime.wasm png2bng tsodinw.bng bngviewer bng.wasm
-
-prime: prime.c
-	$(CC) -O3 -o prime prime.c
-
-prime.wasm: prime.wat
-	wat2wasm prime.wat
+all: png2bng tsodinw.bng bngviewer bng.wasm
 
 
 png2bng: png2bng.c bng.h stb_image.h
 png2bng: png2bng.c bng.h stb_image.h
 	$(CC) -O3 -o png2bng png2bng.c -lm
 	$(CC) -O3 -o png2bng png2bng.c -lm

+ 1 - 27
README.md

@@ -2,33 +2,7 @@
 
 
 - [x] Premise: Performance of WASM is probably comparable to native code
 - [x] Premise: Performance of WASM is probably comparable to native code
   - [x] Let's check it by implementing a Prime Number Crunching as a "Benchmark":
   - [x] Let's check it by implementing a Prime Number Crunching as a "Benchmark":
-    - [x] [Native C Implementation](./prime.c)
-      ```console
-        $ time ./prime
-        179424673
-
-        real    0m52.964s
-        user    0m52.766s
-        sys     0m0.196s
-      ```
-    - [x] [JavaScript Implementation](./prime-naive.js)
-      ```console
-        $ time node prime-naive.js
-        179424673
-
-        real    2m4.781s
-        user    2m4.414s
-        sys     0m0.208s
-      ```
-    - [x] [WebAssembly Implemention](./prime.wat)
-      ```console
-        $ time node prime-wasm.js
-        179424673
-
-        real    1m17.061s
-        user    1m16.844s
-        sys     0m0.192s
-      ```
+    - [x] https://github.com/tsoding/prime-benchmark
 - [ ] Idea: Let's imagine we invented a new image format that didn't exist before
 - [ ] Idea: Let's imagine we invented a new image format that didn't exist before
   - [ ] If we implement the image support in WASM can we make it feel like a native browser support
   - [ ] If we implement the image support in WASM can we make it feel like a native browser support
     - [x] [Implement png2bng](./png2bng.c)
     - [x] [Implement png2bng](./png2bng.c)

+ 0 - 19
prime-naive.js

@@ -1,19 +0,0 @@
-const N = 10 * 1000 * 1000;
-const primes = [2];
-
-function is_prime(x) {
-    for (let i = 0; primes[i] * primes[i] <= x; ++i) {
-        if (x % primes[i] == 0) {
-            return false;
-        }
-    }
-    return true;
-}
-
-for (let x = 3; primes.length < N; ++x) {
-    if (is_prime(x)) {
-        primes.push(x);
-    }
-}
-
-console.log(primes[primes.length - 1]);

+ 0 - 6
prime-wasm.js

@@ -1,6 +0,0 @@
-const fs = require("fs");
-
-WebAssembly
-    .instantiate(new Uint8Array(fs.readFileSync("./prime.wasm")), {})
-    .then((obj) => console.log(obj.instance.exports.prime(10 * 1000 * 1000)))
-    .catch((x) => console.log(x));

+ 0 - 31
prime.c

@@ -1,31 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-
-#define N (10 * 1000 * 1000)
-
-int primes[N] = {2};
-size_t primes_count = 1;
-
-int is_prime(int x)
-{
-    for (size_t i = 0; primes[i] * primes[i] <= x; ++i) {
-        if (x % primes[i] == 0) {
-            return 0;
-        }
-    }
-
-    return 1;
-}
-
-int main(int argc, char *argv[])
-{
-    for (int x = 3; primes_count < N; ++x) {
-        if (is_prime(x)) {
-            primes[primes_count++] = x;
-        }
-    }
-
-    printf("%d\n", primes[primes_count - 1]);
-
-    return 0;
-}

+ 0 - 40
prime.wat

@@ -1,40 +0,0 @@
-(module
- (memory (export "primes") 700 700)
- (func $is_prime
-       (param $x i32)
-       (result i32)
-       (local $prime i32)
-       (local $i i32)
-       (local $result i32)
-       (set_local $i (i32.const 0))
-       (block
-        $exit
-        (loop
-         $top
-         (set_local $prime (i32.load (i32.mul (get_local $i) (i32.const 4))))
-         (set_local $result (i32.const 1))
-         (br_if $exit (i32.gt_s (i32.mul (get_local $prime) (get_local $prime)) (get_local $x)))
-         (set_local $result (i32.const 0))
-         (br_if $exit (i32.eq (i32.rem_s (get_local $x) (get_local $prime)) (i32.const 0)))
-         (set_local $i (i32.add (get_local $i) (i32.const 1)))
-         (br $top)))
-       (get_local $result))
- (func (export "prime")
-       (param $n i32)
-       (result i32)
-       (local $primes_count i32)
-       (local $x i32)
-       (i32.store (i32.const 0) (i32.const 2))
-       (set_local $primes_count (i32.const 1))
-       (set_local $x (i32.const 2))
-       (block
-        $exit
-        (loop
-         $top
-         (set_local $x (i32.add (get_local $x) (i32.const 1)))
-         (br_if $exit (i32.ge_s (get_local $primes_count) (get_local $n)))
-         (br_if $top (i32.eqz (call $is_prime (get_local $x))))
-         (i32.store (i32.mul (get_local $primes_count) (i32.const 4)) (get_local $x))
-         (set_local $primes_count (i32.add (get_local $primes_count) (i32.const 1)))
-         (br $top)))
-       (i32.load (i32.mul (i32.sub (get_local $primes_count) (i32.const 1)) (i32.const 4)))))