5 time faster
jus by using another bignum library for the bulk of the ops. this shit is ass. IM NOT making a from and into pyobj for any demonstration purposes; fuck dat shi
This commit is contained in:
1 parent
18f5766c67
commit
e92eb9030a
7 files changed
+183
-66
No files matched your search
Generated
+114
-19
@@ -14,12 +14,30 @@ version = "2.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
|
||||
|
||||
[[package]]
|
||||
name = "bytemuck"
|
||||
version = "1.23.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c76a5792e44e4abe34d3abf15636779261d45a7450612059293d1d2cfc63422"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
|
||||
|
||||
[[package]]
|
||||
name = "foldhash"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.3.3"
|
||||
@@ -32,6 +50,15 @@ dependencies = [
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.15.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5971ac85611da7067dbfcabef3c70ebb5606018acd9e2a3903a0da507521e0d5"
|
||||
dependencies = [
|
||||
"foldhash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.5.0"
|
||||
@@ -44,12 +71,64 @@ version = "2.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f4c7245a08504955605670dbf141fceab975f15ca21570696aebe9d2e71576bd"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.14.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.172"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa"
|
||||
|
||||
[[package]]
|
||||
name = "libm"
|
||||
version = "0.2.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f9fbbcab51052fe104eb5e5d351cf728d30a5be1fe14d9be8a3b097481fb97de"
|
||||
|
||||
[[package]]
|
||||
name = "malachite-base"
|
||||
version = "0.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c738d3789301e957a8f7519318fcbb1b92bb95863b28f6938ae5a05be6259f34"
|
||||
dependencies = [
|
||||
"hashbrown",
|
||||
"itertools",
|
||||
"libm",
|
||||
"ryu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "malachite-bigint"
|
||||
version = "0.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7f46b904a4725706c5ad0133b662c20b388a3ffb04bda5154029dcb0cd28ae34"
|
||||
dependencies = [
|
||||
"malachite-base",
|
||||
"malachite-nz",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
"paste",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "malachite-nz"
|
||||
version = "0.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1707c9a1fa36ce21749b35972bfad17bbf34cf5a7c96897c0491da321e387d3b"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
"libm",
|
||||
"malachite-base",
|
||||
"wide",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memoffset"
|
||||
version = "0.9.1"
|
||||
@@ -67,7 +146,6 @@ checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9"
|
||||
dependencies = [
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
"rand 0.8.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -105,6 +183,12 @@ version = "1.21.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
|
||||
|
||||
[[package]]
|
||||
name = "paste"
|
||||
version = "1.0.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
||||
|
||||
[[package]]
|
||||
name = "portable-atomic"
|
||||
version = "1.11.1"
|
||||
@@ -207,15 +291,6 @@ version = "5.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||
dependencies = [
|
||||
"rand_core 0.6.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.9.1"
|
||||
@@ -223,7 +298,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97"
|
||||
dependencies = [
|
||||
"rand_chacha",
|
||||
"rand_core 0.9.3",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -233,15 +308,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core 0.9.3",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.9.3"
|
||||
@@ -255,11 +324,27 @@ dependencies = [
|
||||
name = "rpy"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"malachite-bigint",
|
||||
"num-bigint",
|
||||
"num-iter",
|
||||
"num-traits",
|
||||
"pyo3",
|
||||
"rand 0.9.1",
|
||||
"rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
|
||||
|
||||
[[package]]
|
||||
name = "safe_arch"
|
||||
version = "0.7.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "96b02de82ddbe1b636e6170c21be622223aea188ef2e139be0a5b219ec215323"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -300,6 +385,16 @@ dependencies = [
|
||||
"wit-bindgen-rt",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wide"
|
||||
version = "0.7.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0ce5da8ecb62bcd8ec8b7ea19f69a51275e91299be594ea5cc6ef7819e16cd03"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"safe_arch",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wit-bindgen-rt"
|
||||
version = "0.39.0"
|
||||
|
||||
+2
-1
@@ -9,7 +9,8 @@ name = "lda_module"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
num-bigint = { version = "0.4.6", features = ["rand"] }
|
||||
malachite-bigint = "0.6.1"
|
||||
num-bigint = { version = "0.4.6" }
|
||||
num-iter = "0.1.45"
|
||||
num-traits = "0.2.19"
|
||||
pyo3 = { version = "0.25.0", features = ["num-bigint"] }
|
||||
|
||||
+31
-23
@@ -1,15 +1,17 @@
|
||||
import math
|
||||
from random import randint
|
||||
|
||||
# import timeit
|
||||
import fib
|
||||
import lda_module
|
||||
|
||||
print(dir(lda_module.fib))
|
||||
f = {"randfib": lda_module.rand_lda.randfib}
|
||||
# print(dir(lda_module.fib))
|
||||
# f = {"randfib": lda_module.rand_lda.randfib}
|
||||
# g = "92"
|
||||
g = "randfib()"
|
||||
print(g)
|
||||
n = 5 * 10**6
|
||||
print(n)
|
||||
# g = "randfib()"
|
||||
# print(g)
|
||||
# n = 5 * 10**6
|
||||
# print(n)
|
||||
|
||||
# randfib()
|
||||
# 30000000 # 3e7
|
||||
@@ -18,30 +20,36 @@ print(n)
|
||||
# its so good
|
||||
# powered by --release
|
||||
|
||||
# print(timeit.timeit(f"fib({g})", number=n, globals=f | {"fib": fib.fast2}))
|
||||
# print(timeit.timeit(f"fib({g})", number=n, globals=f | {"fib": lda_module.fib.fib}))
|
||||
# print("other fibs")
|
||||
# print(timeit.timeit(f"fib({g})", number=n, globals=f | {"fib": lda_module.fib.fibr}))
|
||||
# print(timeit.timeit(f"fib({g})", number=n, globals=f | {"fib": lda_module.fib.iterfib}))
|
||||
# functions = {
|
||||
# "fast2": fib.fast2,
|
||||
# "fib": lda_module.fib.fib,
|
||||
# "iterfib": lda_module.fib.iterfib,
|
||||
# "randfib": lda_module.rand_lda.randfib,
|
||||
# }
|
||||
# for name, func in functions.items():
|
||||
# print(f"{name}: {timeit.timeit(f'{name}({g})', globals={name: func} | f, number=n)}")
|
||||
|
||||
|
||||
input_number = 8_000_000
|
||||
# input_number = 8_000_000 + randint(-8_000_000, 16_000_000)
|
||||
input_number = 23377166 # testing consistency; its a random number
|
||||
import time
|
||||
|
||||
start = time.perf_counter_ns()
|
||||
r = fib.fast2(input_number)
|
||||
end = time.perf_counter_ns()
|
||||
print(f"did {input_number} in {(end - start) / 1e3} us") # 956078.1
|
||||
print("number has", math.ceil(math.log10(r)), "digits")
|
||||
# start = time.perf_counter_ns()
|
||||
# r = fib.fast2(input_number) # too big number
|
||||
# end = time.perf_counter_ns()
|
||||
# print(f"did {input_number} in {(end - start) / 1e3} us") # 956078.1
|
||||
# print("number has", math.ceil(math.log10(r)), "digits")
|
||||
|
||||
start = time.perf_counter_ns()
|
||||
r = lda_module.fib_big.fast2(input_number) # 10 million times slower # 3332277.8
|
||||
r = lda_module.fib_big.fast2(input_number) # 10 million times slower # 3332277.8
|
||||
end = time.perf_counter_ns()
|
||||
print(f"did {input_number} in {(end - start) / 1e3} us")
|
||||
print("number has", math.ceil(math.log10(r)), "digits")
|
||||
print(f"did {input_number} in {(end - start) / 1e3:_} us")
|
||||
print(f"number has {math.ceil(math.log10(r)):_} digits")
|
||||
|
||||
start = time.perf_counter_ns()
|
||||
r = lda_module.fib_big.f_rec(input_number) # 10 million times slower # 3332277.8
|
||||
r = lda_module.fib_big.f_rec(input_number) # 10 million times slower # 3332277.8
|
||||
end = time.perf_counter_ns()
|
||||
print(f"did {input_number} in {(end - start) / 1e3} us")
|
||||
print("number has", math.ceil(math.log10(r)), "digits")
|
||||
print(f"did {input_number} in {((end - start) / 1e3):_} us")
|
||||
print(f"number has {math.ceil(math.log10(r)):_} digits")
|
||||
|
||||
# fib_big aka fib SLOW omfg
|
||||
+22
-11
@@ -1,13 +1,13 @@
|
||||
use num_bigint::BigUint;
|
||||
use num_traits::One;
|
||||
use malachite_bigint::BigUint;
|
||||
use num_traits::{One, Zero};
|
||||
|
||||
/// num bigint making it awkward
|
||||
pub fn fast2_big(n: BigUint) -> BigUint {
|
||||
let (mut a, mut b) = (BigUint::ZERO, BigUint::one());
|
||||
let (mut a, mut b) = (BigUint::zero(), BigUint::one());
|
||||
let m = n;
|
||||
let mut bit_len = m.bits();
|
||||
loop {
|
||||
let c = &a * (&b * 2u8 - &a);
|
||||
let c = &a * (&b + &b - &a);
|
||||
let d = &a * &a + &b * &b;
|
||||
if !m.bit(bit_len) {
|
||||
(a, b) = (c, d);
|
||||
@@ -26,8 +26,8 @@ pub fn fast2_big(n: BigUint) -> BigUint {
|
||||
|
||||
/// DO NOT USE for it is slow af
|
||||
pub fn iter_bigfib(n: BigUint) -> BigUint {
|
||||
let (mut a, mut b) = (BigUint::ZERO, BigUint::one());
|
||||
for _ in num_iter::range(BigUint::ZERO, n) {
|
||||
let (mut a, mut b) = (BigUint::zero(), BigUint::one());
|
||||
for _ in num_iter::range(BigUint::zero(), n) {
|
||||
(b, a) = (a + &b, b);
|
||||
}
|
||||
a
|
||||
@@ -36,14 +36,14 @@ pub fn iter_bigfib(n: BigUint) -> BigUint {
|
||||
// again not having copy + new upon ops makes this so ASSSS
|
||||
pub fn fib_rec(n: BigUint) -> BigUint {
|
||||
fn fib_pair(n: BigUint) -> (BigUint, BigUint) {
|
||||
if BigUint::ZERO == n {
|
||||
(BigUint::ZERO, BigUint::one())
|
||||
if BigUint::zero() == n {
|
||||
(BigUint::zero(), BigUint::one())
|
||||
} else {
|
||||
let (a, b) = fib_pair(&n / 2u8);
|
||||
let c = &a * (&b * 2u8 - &a);
|
||||
let (a, b) = fib_pair(&n >> 1);
|
||||
let c = &a * (&b + &b - &a);
|
||||
let d = &a * &a + &b * &b;
|
||||
|
||||
if n % 2u8 == BigUint::ZERO {
|
||||
if n % 2u8 == BigUint::zero() {
|
||||
(c, d)
|
||||
} else {
|
||||
let temp = c + &d;
|
||||
@@ -117,3 +117,14 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
pub mod con {
|
||||
use malachite_bigint::BigUint as mbu;
|
||||
use num_bigint::BigUint as nbu;
|
||||
|
||||
pub fn from_m_to_n(n: mbu) -> nbu {
|
||||
nbu::new(n.iter_u32_digits().collect())
|
||||
}
|
||||
pub fn from_n_to_m(n: nbu) -> mbu {
|
||||
mbu::new(n.to_u32_digits())
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -66,8 +66,8 @@ mod tests {
|
||||
let a = fib(n);
|
||||
let b = iterfib(n);
|
||||
let c = fast2(n);
|
||||
assert_eq!(a, b, "a is not equal b; n = {n}");
|
||||
assert_eq!(c, b, "c is not equal b; n = {n}")
|
||||
assert_eq!(a, b, "a does not equal b; n = {n}");
|
||||
assert_eq!(c, b, "c does not equal b; n = {n}")
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
-9
@@ -1,6 +1,6 @@
|
||||
mod big;
|
||||
mod fib;
|
||||
mod rand_lda;
|
||||
mod big;
|
||||
|
||||
mod pyexport {
|
||||
use pyo3::prelude::*;
|
||||
@@ -10,13 +10,13 @@ mod pyexport {
|
||||
#[pymodule]
|
||||
mod fib {
|
||||
use super::*;
|
||||
|
||||
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (n), text_signature = "(n: int) -> int")]
|
||||
const fn fib(n: u32) -> u64 {
|
||||
crate::fib::fast2(n)
|
||||
}
|
||||
|
||||
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (n), text_signature = "(n: int) -> int")]
|
||||
const fn iterfib(n: u32) -> u64 {
|
||||
@@ -31,7 +31,7 @@ mod pyexport {
|
||||
#[pymodule]
|
||||
mod rand_lda {
|
||||
use super::*;
|
||||
|
||||
|
||||
#[pyfunction]
|
||||
#[pyo3(text_signature = "() -> int")]
|
||||
fn randfib() -> u32 {
|
||||
@@ -40,27 +40,29 @@ mod pyexport {
|
||||
}
|
||||
#[pymodule]
|
||||
mod fib_big {
|
||||
|
||||
use super::*;
|
||||
|
||||
|
||||
use crate::big::con::from_m_to_n as mn;
|
||||
use crate::big::con::from_n_to_m as nm;
|
||||
use num_bigint::BigUint;
|
||||
|
||||
///sybau
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (n), text_signature = "(n: int) -> int")]
|
||||
fn fast2(n: BigUint) -> BigUint {
|
||||
crate::big::fast2_big(n)
|
||||
mn(crate::big::fast2_big(nm(n)))
|
||||
}
|
||||
///iter. DO NOT USE
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (n), text_signature = "(n: int) -> int")]
|
||||
fn iter(n: BigUint) -> BigUint {
|
||||
crate::big::iter_bigfib(n)
|
||||
mn(crate::big::iter_bigfib(nm(n)))
|
||||
}
|
||||
///recursion.
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (n), text_signature = "(n: int) -> int")]
|
||||
fn f_rec(n: BigUint) -> BigUint {
|
||||
crate::big::fib_rec(n)
|
||||
mn(crate::big::fib_rec(nm(n)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -3,4 +3,4 @@
|
||||
/// the number is 92. number is 0 to 92.
|
||||
pub fn randfib() -> u32 {
|
||||
rand::random_range(0..93)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user