Skip to content

Instantly share code, notes, and snippets.

View niczky12's full-sized avatar
🖊️

Bence Komarniczky niczky12

🖊️
View GitHub Profile
@niczky12
niczky12 / bigquery_deploy.yml
Created March 21, 2023 10:08
deploying bigquery functions via github actions
on:
push:
branches:
- main
env:
GCP_PROJECT_ID: your_project_id
BQ_DATASET_ID: your_dataset_id
# 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),
romeoAndJuliet = @pipe "https://shakespeare.folger.edu/downloads/txt/romeo-and-juliet_TXT_FolgerShakespeare.txt" |>
download |>
readlines |>
reduce(*, _)
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']
)
@niczky12
niczky12 / ismultiple12.jl
Last active December 8, 2022 22:18
Silly Julia function for detecting multiples of 12
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)
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
# 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)
using Distributed
function sumupto_dist(fun, N)
@distributed (+) for i in 1:N
fun(i)
end
end
using Base.Threads
function sumupto(fun, n)
s = 0
Threads.@threads for i in 1:n
s += fun(i)
end
s
end
using BenchmarkTools
# benchmarking single thread performance
N = 10_000
# 8.167 μs
@btime sum(ismultiple12.(1:N))
# without boradcasting, even faster!
# 5.800 μs