Comment

Comment by buster

I was curious to reproduce the "benchmark" in rust, which got me to this code. Mind you, i have not much experienace in rust, so i'd think this would be the most straight forward translation of the nim code in the blog post for a beginner.

  use std::fs::File;
  use std::io::Read;
  use std::path::Path;
  use serde_json::Value;
  
  fn main() -> Result<(), Box<dyn std::error::Error>> {
      let mut json_file = File::open(Path::new("1.json"))?;
      let mut json_content = String::new();
      json_file.read_to_string(&mut json_content)?;

      let json_value : Value = serde_json::from_str(json_content.as_str())?;
      let coords = json_value["coordinates"].as_array().expect("Coordinates");
      let len: f64 = coords.len() as f64;
      let mut x: f64 = 0.0;
      let mut y: f64 = 0.0;
      let mut z: f64 = 0.0;
  
      for value in coords {
          x = x + &value["x"].as_f64().expect("X Conversion");
          y = y + &value["y"].as_f64().expect("y Conversion");
          z = z + &value["z"].as_f64().expect("z Conversion");
      }
  
      println!("{}", x / len);
      println!("{}", z / len);
      println!("{}", z / len);

      Ok(())
  }
(fyi: the program ran in 2 seconds on my 7 year old laptop)
parent buster 6 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton