-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bench serde-json using simd-json (#426)
- Loading branch information
1 parent
c470d59
commit 791293a
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
use std::fs; | ||
|
||
fn main() -> anyhow::Result<()> { | ||
let file_name = std::env::args_os() | ||
.nth(1) | ||
.and_then(|s| s.into_string().ok()) | ||
.unwrap_or("sample".to_string()); | ||
let n = std::env::args_os() | ||
.nth(2) | ||
.and_then(|s| s.into_string().ok()) | ||
.and_then(|s| s.parse().ok()) | ||
.unwrap_or(10); | ||
let mut json_str = fs::read(format!("{}.json", file_name))?; | ||
let json: GeoData = simd_json::from_slice(&mut json_str)?; | ||
|
||
print_hash(simd_json::to_vec(&json)?); | ||
let mut array = Vec::with_capacity(n); | ||
for _i in 0..n { | ||
let json: GeoData = simd_json::from_slice(&mut json_str)?; | ||
array.push(json); | ||
} | ||
print_hash(simd_json::to_vec(&array)?); | ||
Ok(()) | ||
} | ||
|
||
fn print_hash(bytes: impl AsRef<[u8]>) { | ||
let digest = md5::compute(&bytes); | ||
println!("{:x}", digest); | ||
} | ||
|
||
#[derive(Deserialize, Debug, Default)] | ||
struct MyF64(f64); | ||
|
||
impl Serialize for MyF64 { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
if self.0.fract() == 0.0 { | ||
serializer.serialize_i64(self.0 as i64) | ||
} else { | ||
serializer.serialize_f64(self.0) | ||
} | ||
} | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Debug, Default)] | ||
struct GeoData { | ||
r#type: String, | ||
features: Vec<Feature>, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Debug, Default)] | ||
struct Feature { | ||
r#type: String, | ||
properties: Properties, | ||
geometry: Geometry, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Debug, Default)] | ||
struct Properties { | ||
name: String, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Debug, Default)] | ||
struct Geometry { | ||
r#type: String, | ||
coordinates: Vec<Vec<[MyF64; 2]>>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,7 @@ problems: | |
- 1.rs | ||
- 2.rs | ||
- 3.rs | ||
- 4.rs | ||
- name: coro-prime-sieve | ||
source: | ||
- 1.rs | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters