Skip to content

Instantly share code, notes, and snippets.

@octohedron
Created August 17, 2022 22:05
Show Gist options
  • Save octohedron/4f93ad4767ee2ffe95ae98125b0bff92 to your computer and use it in GitHub Desktop.
Save octohedron/4f93ad4767ee2ffe95ae98125b0bff92 to your computer and use it in GitHub Desktop.
strange transpose times
use std::time;
fn get_test_matrix() -> Vec<Vec<f64>> {
let mut x: Vec<Vec<f64>> = vec![vec![0.; 790]; 500000];
for i in 0..x.len() {
for j in 0..x[0].len() {
x[i][j] = fastrand::f64() * 500.;
}
}
x
}
fn tb(a: &Vec<Vec<f64>>) -> Vec<Vec<f64>> {
let mut b: Vec<Vec<f64>> = vec![vec![0.; a.len()]; a[0].len()];
for j in 0..a[0].len() {
for i in 0..a.len() {
b[j][i] = a[i][j];
}
}
b
}
fn ta(a: &Vec<Vec<f64>>) -> Vec<Vec<f64>> {
let mut b: Vec<Vec<f64>> = vec![Vec::with_capacity(a.len()); a[0].len()];
for r in a {
for i in 0..r.len() {
b[i].push(r[i]);
}
}
b
}
fn main() {
let a = get_test_matrix();
let b = get_test_matrix();
let time_a = time::Instant::now();
ta(&a);
println!("ta -> {}s", time_a.elapsed().as_secs());
let time_b = time::Instant::now();
tb(&b);
println!("tb -> {}s", time_b.elapsed().as_secs());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment