Skip to content

Instantly share code, notes, and snippets.

@dvzubarev
Last active February 13, 2020 10:14
Show Gist options
  • Save dvzubarev/e153a437305066bb1aa1f4865bbbeb1d to your computer and use it in GitHub Desktop.
Save dvzubarev/e153a437305066bb1aa1f4865bbbeb1d to your computer and use it in GitHub Desktop.
#include <jsoncons/json.hpp>
#include <jsoncons_ext/cbor/cbor.hpp>
#include <fstream>
#include <chrono>
using namespace jsoncons;
int main()
{
std::ifstream is("/tmp/bench-bytes", std::ios::binary);
if (not is){
std::cerr<<"failed to open file!"<<std::endl;
return 1;
}
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);
std::vector<uint8_t> buf(length);
is.read(reinterpret_cast<char*>(buf.data()), length);
std::cout<<"buf size: "<<buf.size()<<std::endl;
auto t1 = std::chrono::high_resolution_clock::now();
auto v = cbor::decode_cbor<std::vector<float>>(buf);
auto t2 = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>( t2 - t1 ).count();
std::cout<<"dec time: " << duration<<" ms"<<std::endl;
std::cout << "dec length "<<v.size()<<std::endl;
std::cout<<"dec first 5 elems:\n";
for (int i = 0; i < 5; ++i){
std::cout << std::defaultfloat << v[i] << "\n";
}
}
#!/usr/bin/env python
# coding: utf-8
import time
import sys
import cbor2
import numpy as np
r = np.random.rand(13100100).astype(np.float32)
print ("length: %d" % len(r))
print ("first 5 elems: %s" % r[:5])
def float32_encoder(encoder, value):
assert value.dtype == np.dtype('float32')
assert value.dtype.byteorder == '='
assert sys.byteorder == 'little'
encoder.encode(cbor2.CBORTag(85, value.tobytes()))
bt = time.time()
encoded = cbor2.dumps(r, default = float32_encoder)
print ("enc time: %s" % (time.time() - bt))
with open('/tmp/bench-bytes', 'wb') as f:
f.write(encoded)
def typed_arr_decoder(decoder, tag, shareable_index=None):
if tag.tag not in (85, ) :
return tag
if tag.tag == 85:
assert sys.byteorder == 'little'
return np.frombuffer(tag.value, np.float32)
bt = time.time()
dec_r = cbor2.loads(encoded, tag_hook = typed_arr_decoder)
print ("dec time: %s" % (time.time() - bt))
print ("dec length: %d" % len(dec_r))
print ("dec first 5 elems: %s" % dec_r[:5])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment