Last active
December 10, 2015 12:54
-
-
Save domgetter/07915405027a63efc4af to your computer and use it in GitHub Desktop.
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
// Do note that the following java code takes about 8-15 microseconds also | |
public class Timing { | |
public static void main(String[] Args) { | |
for(int i = 0; i < 10000; i++){ // warmup | |
thing(); | |
} | |
long start_time = System.nanoTime(); | |
thing(); | |
System.out.println(System.nanoTime() - start_time); // tends to print out 8000-15000 which is 8-15 microseconds | |
} | |
public static long thing(){ | |
double zre = 0; | |
double zim = 0; | |
double cre = -1.08; | |
double cim = 0.07; | |
double new_zre = 0; | |
double new_zim = 0; | |
long dwell = 2001; | |
for(long i = 0; i < 2000; i++){ // this is not an attempt at calculating the its, just make the formula run 2000 times | |
if(zre*zre + zim*zim > 4.0 || i == dwell) { | |
break; | |
} else { | |
new_zre = zre*zre - zim*zim + cre; | |
new_zim = 2.0*zre*zim + cim; | |
zre = new_zre; | |
zim = new_zim; | |
i = i; | |
dwell = dwell; | |
} | |
} | |
return 2000; | |
} | |
} |
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
;; Winner winner, chicken dinner. This calcs (mandel (complex. 0 0) 2000) in 15 μs after warmup | |
(deftype complex [^double real ^double imag]) | |
(defn plus | |
"(a + bi) + (c + di) == (a + b) + (c + d)i" | |
[^complex z1 ^complex z2] | |
(complex. (+ (.real z1) (.real z2)) (+ (.imag z1) (.imag z2)))) | |
(defn magnitude-squared | |
"|z|^2 == a^2 + b^2" | |
[^complex z] | |
(+ (* (.real z) (.real z)) (* (.imag z) (.imag z)))) | |
(defn times | |
"(a + bi)*(c + di) == (ac - bd) + (ad + bc)i" | |
[^complex z1 ^complex z2] | |
(complex. | |
(- (* (.real z1) (.real z2)) (* (.imag z1) (.imag z2))) | |
(+ (* (.real z1) (.imag z2)) (* (.imag z1) (.real z2))))) | |
(defn mandel | |
"Calculates the number of iterations taken to escape, up to a bailout | |
(mandel (complex. 0 0) 100) => 100 since [0,0] is in the set" | |
([^complex c ^long dwell] | |
(mandel (complex. 0 0) c 0 dwell)) | |
([z c ^long its ^long dwell] | |
(if (or (> (magnitude-squared z) 4.0) (= its dwell)) | |
its | |
(recur (plus (times z z) c) c (inc its) dwell)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment