Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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