Skip to content

Instantly share code, notes, and snippets.

@DanielGibson
Created August 2, 2016 21:56
Show Gist options
  • Save DanielGibson/6341a3637f1ba7fa820bd540c759db37 to your computer and use it in GitHub Desktop.
Save DanielGibson/6341a3637f1ba7fa820bd540c759db37 to your computer and use it in GitHub Desktop.
Test rapidjson parsing speed, like in http://computers-are-fast.github.io/
// this is hacky and assumes messages.json is <64k bytes. that's the case for
// https://github.com/kamalmarhubi/one-second/blob/master/setup/protobuf/message.json
#include "rapidjson/document.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
size_t numIterations = 1;
if(argc > 1)
numIterations = atoi(argv[1]);
char buf[64*1024];
FILE* f = fopen("message.json", "r");
size_t size = fread(buf, 1, sizeof(buf), f);
fclose(f);
buf[size++] = '\0';
char jbuf[sizeof(buf)];
for(size_t i=0; i<numIterations; ++i)
{
rapidjson::Document document;
#if 1 // test insitu, change to "#if 0" for nondestructive parsing (which takes ~20-25% longer)
// as the buffer will be modified by ParseInsitu(),
// copy it from orig json string
memcpy(jbuf, buf, size);
bool hasError = document.ParseInsitu(jbuf).HasParseError();
#else
bool hasError = document.Parse(buf).HasParseError();
#endif
// make sure the code above (=> this loop) is not
// optimized away by telling the compiler hasError
// is important in a away it doesn't understand
asm volatile("" : "+r" (hasError));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment