Advertisement
Guest User

benchmark.d

a guest
May 14th, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.43 KB | None | 0 0
  1. import std.datetime;
  2. import std.stdio;
  3. import std.file;
  4. import std.algorithm;
  5.  
  6. import stdJSON = std.json;
  7. import vibeJSON = vibe.data.json;
  8. import myJSON = json;
  9.  
  10. immutable size_t array_size = 1000;
  11. immutable int runs = 100;
  12. immutable size_t chunk_size = 4096;
  13.  
  14. string jsonString;
  15.  
  16. void buildTestData() {
  17.     auto obj = myJSON.jsonObject();
  18.  
  19.     obj["a"] = "foo";
  20.     obj["b"] = "bar";
  21.     obj["c"] = "baz";
  22.  
  23.     auto subobj1 = myJSON.jsonObject();
  24.  
  25.     subobj1["bla"] = 1234.5f;
  26.  
  27.     obj["sub"] = subobj1;
  28.  
  29.     auto subobj2 = myJSON.jsonObject();
  30.  
  31.     subobj2["alb"] = null;
  32.  
  33.     subobj1["sub"] = subobj2;
  34.  
  35.     auto arr = myJSON.jsonArray();
  36.     arr.length = array_size;
  37.  
  38.     for (size_t i = 0; i < array_size; ++i) {
  39.         arr[i] = obj;
  40.     }
  41.  
  42.     jsonString = myJSON.toJSON(arr);
  43. }
  44.  
  45. void readArrayStandard() {
  46.     stdJSON.parseJSON(jsonString);
  47. }
  48.  
  49. void readArrayVibe() {
  50.     vibeJSON.parseJsonString(jsonString);
  51. }
  52.  
  53. void readArrayMyWay() {
  54.     myJSON.parseJSON(jsonString);
  55. }
  56.  
  57. void main(string[] argv) {
  58.     buildTestData();
  59.  
  60.     auto testList = [
  61.         "std.json",
  62.         "vibe.data.json",
  63.         "json"
  64.     ];
  65.  
  66.     auto result = benchmark!(
  67.         readArrayStandard,
  68.         readArrayVibe,
  69.         readArrayMyWay
  70.     )(runs);
  71.  
  72.     writefln("Ran for %d runs\n", runs);
  73.  
  74.     foreach(i, testName; testList) {
  75.         writefln("%s : %s ms", testName, result[i].msecs);
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement