Skip to content

Instantly share code, notes, and snippets.

@anowell
Last active November 1, 2016 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anowell/349b787d47b16297cb55a302fd777faa to your computer and use it in GitHub Desktop.
Save anowell/349b787d47b16297cb55a302fd777faa to your computer and use it in GitHub Desktop.
Basic microbench of decoding large arrays of zeroes
#![feature(test)]
extern crate serde_json;
extern crate json;
extern crate test;
#[cfg(test)]
mod tests {
use json::{self, JsonValue};
use serde_json::{self, Value};
use test::Bencher;
#[inline]
fn decode_serde(json :&str) -> Value {
serde_json::from_str(json).expect("failed to decode json")
}
#[inline]
fn decode_json(json :&str) -> JsonValue {
json::parse(json).expect("failed to decode json")
}
#[inline]
fn encode(arr: &Vec<u32>) -> String {
serde_json::to_string(arr).expect("failed to encode json")
}
#[bench]
fn bench_json_1k_zeroes(b: &mut Bencher) {
let json: String = encode(&vec![0; 1_000]);
b.iter(|| decode_json(&json))
}
#[bench]
fn bench_json_1m_zeroes(b: &mut Bencher) {
let json: String = encode(&vec![0u32; 1_000_000]);
b.iter(|| decode_json(&json))
}
#[bench]
fn bench_json_10m_zeroes(b: &mut Bencher) {
let json: String = encode(&vec![0u32; 10_000_000]);
b.iter(|| decode_json(&json))
}
#[bench]
fn bench_serde_1k_zeroes(b: &mut Bencher) {
let json: String = encode(&vec![0; 1_000]);
b.iter(|| decode_serde(&json))
}
#[bench]
fn bench_serde_1m_zeroes(b: &mut Bencher) {
let json: String = encode(&vec![0u32; 1_000_000]);
b.iter(|| decode_serde(&json))
}
#[bench]
fn bench_serde_10m_zeroes(b: &mut Bencher) {
let json: String = encode(&vec![0u32; 10_000_000]);
b.iter(|| decode_serde(&json))
}
/* Takes too long to run
#[bench]
fn bench_serde_100m_zeroes(b: &mut Bencher) {
let json: String = encode(&vec![0u32; 100_000_000]);
b.iter(|| decode_serde(&json))
}
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment