I have never run any benchmarking tests before, let alone in a #![no_std] implementation. I'm implementing some security features and I want to run a prime based algorithm with security features on and with them off. That way I can determine the performance hit of using security measures. I found a primes crate, but I was a little concerned with the following line
let mut pset = Sieve::new();
which makes me think this uses heap. My project does not have an allocator so I can't use heap. Does anyone have any suggestions and/or experience?
There’s this very straightforward way of telling that the heap is used by a type: On the docs page of Sieve, you press the little “[src]” button in the top right corner, and you’ll see that indeed, the Sieve contains a Vec. No need to guess here, it clearly uses the heap. Moreover the crate doesn’t even support no-std.
So after a bit of digging, I’m having a hard time finding a crate for primes that works without allocations. I’ve come across num-primes which at least supports no-std, but relies on the alloc crate being used instead. I’m personally not experienced with Rust on microcontrollers and I don’t know how common/realistic it is to use alloc with a custom global allocator there.
The heapless crate will work I believe, but I really don't need to be able to change the max number for what I'm doing. I can just used a fixed sized mutable array.
fn main() {
let mut primes: [usize;3001] = [0;3001];
for i in 2..=primes.len()-1 {
primes[i] = i;
}
for i in 0..primes.len() {
let factor = primes[i];
if factor != 0 {
sieve(&mut primes, factor);
}
}
for i in 0..primes.len() {
if primes[i] != 0 {
println!("{}", primes[i])
}
}
}
fn sieve(primes: &mut [usize], factor: usize) {
for i in 0..primes.len() {
let value = primes[i];
if value != 0 && value != factor {
if value % factor == 0 {
primes[i] = 0;
}
}
}
}