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
| on: | |
| push: | |
| branches: | |
| - main | |
| env: | |
| GCP_PROJECT_ID: your_project_id | |
| BQ_DATASET_ID: your_dataset_id | |
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
| # single threaded version | |
| score(str) = mapreduce( | |
| letter -> get(scores, letter, 0), | |
| +, | |
| uppercase(str), | |
| init=0) | |
| # same exact thing, except we use ThreadsX | |
| score_parallel(str) = ThreadsX.mapreduce( | |
| letter -> get(scores, letter, 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
| romeoAndJuliet = @pipe "https://shakespeare.folger.edu/downloads/txt/romeo-and-juliet_TXT_FolgerShakespeare.txt" |> | |
| download |> | |
| readlines |> | |
| reduce(*, _) |
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
| value_to_letters = Dict( | |
| 1 => ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'], | |
| 2 => ['D', 'G'], | |
| 3 => ['B', 'C', 'M', 'P'], | |
| 4 => ['F', 'H', 'V', 'W', 'Y'], | |
| 5 => ['K', ], | |
| 8 => ['J', 'X'], | |
| 10 => ['Q', 'Z'] | |
| ) | |
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
| using Test | |
| ismultiple(n, d) = n % d == 0 | |
| ismultiple12(n) = ismultiple(n, 12) | |
| @test ismultiple12(12) | |
| @test ismultiple12(36) | |
| @test !ismultiple12(37) | |
| @test isapprox(sum(ismultiple12.(1:10000)), 10000/12; atol=0.5) |
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
| import Primes | |
| num_prime_divisors(n) = length(keys(Primes.factor(n))) | |
| @test num_prime_divisors(1) == 0 | |
| @test num_prime_divisors(2) == 1 | |
| @test num_prime_divisors(3) == 1 | |
| @test num_prime_divisors(4) == 1 | |
| @test num_prime_divisors(12) == 2 |
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
| # first intstall the package inside the pacakge manager `]` | |
| # add ThreadsX | |
| using ThreadsX | |
| # single core version | |
| sum(ismultiple12, 1:N) | |
| # ThreadsX version | |
| ThreadsX.sum(ismultiple12, 1:N) |
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
| using Distributed | |
| function sumupto_dist(fun, N) | |
| @distributed (+) for i in 1:N | |
| fun(i) | |
| end | |
| end |
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
| using Base.Threads | |
| function sumupto(fun, n) | |
| s = 0 | |
| Threads.@threads for i in 1:n | |
| s += fun(i) | |
| end | |
| s | |
| end |
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
| using BenchmarkTools | |
| # benchmarking single thread performance | |
| N = 10_000 | |
| # 8.167 μs | |
| @btime sum(ismultiple12.(1:N)) | |
| # without boradcasting, even faster! | |
| # 5.800 μs |
NewerOlder