Last active
December 27, 2020 22:34
-
-
Save joshdurbin/3a12d44ab120a10579514c00fa66833a to your computer and use it in GitHub Desktop.
Flippies
This file contains hidden or 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
Programmatic finished in 687498 ms | |
Programmatic finished in 1152935 ms | |
Ran calculations 1000000000 times. Results: | |
|-----------------------------------| | |
| Flips | Programmatic | Mathy | | |
|-----------------------------------| | |
| 0 | 499999527 | 499991146 | | |
| 1 | 249993072 | 249998627 | | |
| 2 | 125001055 | 125011612 | | |
| 3 | 62492746 | 62498907 | | |
| 4 | 31256879 | 31251061 | | |
| 5 | 15627678 | 15629935 | | |
| 6 | 7814956 | 7810012 | | |
| 7 | 3905760 | 3904077 | | |
| 8 | 1953173 | 1950435 | | |
| 9 | 977938 | 977145 | | |
| 10 | 488813 | 488830 | | |
| 11 | 243709 | 244128 | | |
| 12 | 122460 | 122031 | | |
| 13 | 61021 | 61414 | | |
| 14 | 30625 | 30170 | | |
| 15 | 15301 | 15196 | | |
| 16 | 7729 | 7782 | | |
| 17 | 3838 | 3812 | | |
| 18 | 1938 | 1881 | | |
| 19 | 965 | 913 | | |
| 20 | 476 | 467 | | |
| 21 | 252 | 246 | | |
| 22 | 119 | 148 | | |
| 23 | 52 | 75 | | |
| 24 | 55 | 33 | | |
| 25 | 36 | 12 | | |
| 26 | 9 | 38 | | |
| 27 | 2 | 27 | | |
| 28 | 28 | 1 | | |
| 29 | 0 | 29 | | |
| 31 | 0 | 31 | | |
| 32 | 32 | 0 | | |
|-----------------------------------| |
This file contains hidden or 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
➜ ~ groovy programmatic_flip.groovy | |
Programmatic finished in 20046 ms | |
Mathy finished in 66939 ms | |
Ran calculations 100000000 times. Results: | |
|-----------------------------------| | |
| Flips | Programmatic | Mathy | | |
|-----------------------------------| | |
| 0 | 49995423 | 49995644 | | |
| 1 | 25003918 | 25003168 | | |
| 2 | 12499532 | 12501003 | | |
| 3 | 6249696 | 6250883 | | |
| 4 | 3126074 | 3125518 | | |
| 5 | 1562236 | 1562134 | | |
| 6 | 782222 | 780953 | | |
| 7 | 390292 | 391219 | | |
| 8 | 195191 | 194695 | | |
| 9 | 97552 | 97459 | | |
| 10 | 49032 | 48510 | | |
| 11 | 24383 | 24274 | | |
| 12 | 12433 | 12193 | | |
| 13 | 6039 | 6206 | | |
| 14 | 3016 | 3128 | | |
| 15 | 1589 | 1511 | | |
| 16 | 730 | 766 | | |
| 17 | 359 | 369 | | |
| 18 | 191 | 223 | | |
| 19 | 105 | 101 | | |
| 20 | 71 | 49 | | |
| 21 | 51 | 21 | | |
| 22 | 32 | 11 | | |
| 23 | 34 | 2 | | |
| 24 | 1 | 26 | | |
| 25 | 25 | 1 | | |
| 26 | 2 | 27 | | |
| 28 | 0 | 28 | | |
|-----------------------------------| |
This file contains hidden or 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
@Grapes( | |
@Grab(group='com.google.guava', module='guava', version='30.0-jre') | |
) | |
import java.util.Random | |
import java.util.concurrent.ConcurrentHashMap | |
import java.util.concurrent.CountDownLatch | |
import java.util.concurrent.TimeUnit | |
import com.google.common.base.Stopwatch | |
import groovy.transform.Canonical | |
def totalsHistogram = new ConcurrentHashMap() | |
def occurrences = 100_000_000 | |
def latch = new CountDownLatch(2) | |
@Canonical | |
class Totals { | |
def programmatic = 0 | |
def mathy = 0 | |
} | |
Thread.start { | |
def stopwatch = Stopwatch.createStarted() | |
def random = new Random() | |
occurrences.times { | |
def flip = true | |
def count = 0 | |
while(flip) { | |
if (random.nextBoolean()) { | |
count++ | |
} else { | |
break | |
} | |
} | |
if (totalsHistogram.containsKey(count)) { | |
++totalsHistogram.get(count).programmatic | |
} else { | |
totalsHistogram.put(count, new Totals(programmatic: count)) | |
} | |
} | |
println "Programmatic finished in ${stopwatch.elapsed(TimeUnit.MILLISECONDS)} ms" | |
latch.countDown() | |
} | |
Thread.start { | |
def stopwatch = Stopwatch.createStarted() | |
def random = new Random() | |
def pointFiveLN = Math.log(0.5) | |
occurrences.times { | |
count = (Math.log(random.nextDouble()) / pointFiveLN) as Integer | |
if (totalsHistogram.containsKey(count)) { | |
++totalsHistogram.get(count).mathy | |
} else { | |
totalsHistogram.put(count, new Totals(mathy: count)) | |
} | |
} | |
println "Mathy finished in ${stopwatch.elapsed(TimeUnit.MILLISECONDS)} ms" | |
latch.countDown() | |
} | |
latch.await() | |
println "Ran calculations ${occurrences} times. Results:" | |
println '' | |
println '|-----------------------------------|' | |
println '| Flips | Programmatic | Mathy |' | |
println '|-----------------------------------|' | |
def formatOutput = '| %-5s | %-12s | %-10s |%n' | |
totalsHistogram.keySet().sort().each { key -> | |
def totals = totalsHistogram.get(key) | |
System.out.format(formatOutput, key, totals.programmatic, totals.mathy) | |
} | |
println "|-----------------------------------|" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment