using the superior declarative module + add more to test

+ const for no reason (some functions cant even be const
This commit is contained in:
lda committed 2025-06-10 17:02:42 +07:00
1 parent 69a48bc711
commit c690410c8c
7 files changed
+188 -33

No files matched your search

Generated
+107
View File
@@ -8,6 +8,30 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
name = "bitflags"
version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
[[package]]
name = "cfg-if"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
[[package]]
name = "getrandom"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
dependencies = [
"cfg-if",
"libc",
"r-efi",
"wasi",
]
[[package]]
name = "heck"
version = "0.5.0"
@@ -47,6 +71,15 @@ version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f84267b20a16ea918e43c6a88433c2d54fa145c92a811b5b047ccbe153674483"
[[package]]
name = "ppv-lite86"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
dependencies = [
"zerocopy",
]
[[package]]
name = "proc-macro2"
version = "1.0.95"
@@ -127,11 +160,47 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "r-efi"
version = "5.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5"
[[package]]
name = "rand"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97"
dependencies = [
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
dependencies = [
"getrandom",
]
[[package]]
name = "rpy"
version = "0.1.0"
dependencies = [
"pyo3",
"rand",
]
[[package]]
@@ -162,3 +231,41 @@ name = "unindent"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7264e107f553ccae879d21fbea1d6724ac785e8c3bfc762137959b5802826ef3"
[[package]]
name = "wasi"
version = "0.14.2+wasi-0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3"
dependencies = [
"wit-bindgen-rt",
]
[[package]]
name = "wit-bindgen-rt"
version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
dependencies = [
"bitflags",
]
[[package]]
name = "zerocopy"
version = "0.8.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1702d9583232ddb9174e01bb7c15a2ab8fb1bc6f227aa1233858c351a3ba0cb"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28a6e20d751156648aa063f3800b706ee209a32c0b4d9f24be3d980b01be55ef"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
+2 -1
View File
@@ -9,4 +9,5 @@ name = "lda_module"
crate-type = ["cdylib"]
[dependencies]
pyo3 = "0.25.0"
pyo3 = "0.25.0"
rand = "0.9.1"
-6
View File
@@ -12,9 +12,3 @@ def fast2(n: int) -> int:
a, b = d, c + d
mask >>= 1
return a
+19 -6
View File
@@ -1,11 +1,24 @@
import timeit
import fib
import lda_module
import random
f = {"randint": random.randint}
g = "92"
# g = "randint(0, 92)"
print(dir(lda_module.fib))
f = {"randfib": lda_module.rand_lda.randfib}
# g = "92"
g = "randfib()"
print(g)
print(timeit.timeit(f"fast2({g})", globals=f | {"fast2": fib.fast2})) # 0.69
print(timeit.timeit(f"fib({g})", globals=f | {"fib": lda_module.fib})) # 0.31
n = 5 * 10**6
print(n)
# randfib()
# 30000000 # 3e7
# 16.457931599987205
# 1.4654793000081554
# 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}))
+20 -7
View File
@@ -2,10 +2,10 @@
#![allow(unused)]
/// give me a fibonacci number NOW
pub fn fast2(n: u64) -> u64 {
pub const fn fast2(n: u32) -> u64 {
let (mut a, mut b) = (0, 1);
let m = n;
let bit_len = u64::BITS - m.leading_zeros(); // copilot
let bit_len = u32::BITS - m.leading_zeros(); // copilot
let mut mask = if bit_len == 0 { 0 } else { 1 << (bit_len - 1) };
while mask > 0 {
let c = a * (2 * b - a);
@@ -22,16 +22,17 @@ pub fn fast2(n: u64) -> u64 {
// 0, 1, 3, 7
// -> -1 /2 -1 /2 -1 /2
// ->
pub fn iterfib(n: u64) -> u64 {
pub const fn iterfib(mut n: u32) -> u64 {
let (mut a, mut b) = (0, 1);
for _ in 1..n {
while n > 0 {
(a, b) = (b, a + b);
n -= 1; // do this to be const
}
b
a
}
/// ai code
pub fn fib(n: u64) -> u64 {
fn fib_pair(n: u64) -> (u64, u64) {
pub const fn fib(n: u32) -> u64 {
const fn fib_pair(n: u32) -> (u64, u64) {
if (n) == 0 {
(0, 1)
} else {
@@ -49,3 +50,15 @@ pub fn fib(n: u64) -> u64 {
fib_pair(n).0
}
#[test]
fn it_works() {
for _ in 0..100 {
let n = crate::rand_lda::randfib();
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}")
}
}
+34 -13
View File
@@ -1,20 +1,41 @@
mod fib;
mod rand_lda;
pub mod pyexport {
mod pyexport {
use pyo3::prelude::*;
#[pymodule]
mod lda_module {
use super::*;
#[pymodule]
mod fib {
use super::*;
/// this shit is the thing now in python
#[pyfunction]
#[pyo3(signature = (n), text_signature = "(n: int) -> int")]
fn fib(n: u64) -> u64 {
crate::fib::fast2(n)
// currently use u64 as a large number tupe, can use num_bigint
}
#[pyfunction]
#[pyo3(signature = (n), text_signature = "(n: int) -> int")]
const fn fib(n: u32) -> u64 {
crate::fib::fast2(n)
}
/// A Python module implemented in Rust.
#[pymodule(name = "lda_module")]
pub fn what_the_hell(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(fib, m)?)?;
Ok(())
#[pyfunction]
#[pyo3(signature = (n), text_signature = "(n: int) -> int")]
const fn iterfib(n: u32) -> u64 {
crate::fib::iterfib(n)
}
#[pyfunction]
#[pyo3(signature = (n), text_signature = "(n: int) -> int")]
const fn fibr(n: u32) -> u64 {
crate::fib::fib(n)
}
}
#[pymodule]
mod rand_lda {
use super::*;
#[pyfunction]
#[pyo3(text_signature = "() -> int")]
fn randfib() -> u32 {
crate::rand_lda::randfib()
}
}
}
}
+6
View File
@@ -0,0 +1,6 @@
//! rand specific impl
/// the number is 92. number is 0 to 92.
pub fn randfib() -> u32 {
rand::random_range(0..93)
}