1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

bench: add a small performance comparison script bench_json_vs_json2.v

This commit is contained in:
Delyan Angelov 2022-12-12 10:26:04 +02:00
parent b682e9ec0b
commit 992621bd91
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED

View File

@ -0,0 +1,54 @@
import os
import json
import x.json2
import time
import benchmark
// assert json.decode[StructType[time.Time]]('{"val": "2022-03-11T13:54:25.000Z"}')!.val == fixed_time
struct Person {
name string
age int
created_at time.Time
}
fn main() {
max_iterations := os.getenv_opt('MAX_ITERATIONS') or { '1000' }.int()
s := '{"name":"Bilbo Baggins","age":99,"created_at":1670840340}'
mut b := benchmark.start()
for _ in 0 .. max_iterations {
p := json2.decode[Person](s)!
if p.age != 99 {
println('error: ${p}')
}
}
b.measure('json2.decode')
for _ in 0 .. max_iterations {
p := json.decode(Person, s)!
if p.age != 99 {
println('error: ${p}')
}
}
b.measure('json.decode')
// encoding measurements:
p := json.decode(Person, s)!
for _ in 0 .. max_iterations {
es := json2.encode(p)
if es[0] != `{` {
println('json2.encode error: ${es}')
}
}
b.measure('json2.encode')
for _ in 0 .. max_iterations {
es := json.encode(p)
if es[0] != `{` {
println('json.encode error: ${es}')
}
}
b.measure('json.encode')
}