When I was reading the documentation I learned that it supported 2 different formats for loading the configuration, JSON and YAML, and I asked myself (and my friends) which will be a better option. I personally like more using curly braces like in JSON but YAML has somre pretty badass features too so I just decided to use the most efficient one. We thought the most efficient one would be JSON because it seems easier to parse but we weren't completely sure so one of my friends thought that making a little benchmark would be a good idea, so I did.
I created a simple yaml file, then I created a json file from it and after that I created another json file compressing the previous one (taking of all the whitespaces and line breaks). And I created the following code:
import 'dart:convert'; import 'dart:io'; import 'package:benchmark_harness/benchmark_harness.dart'; import 'package:yaml/yaml.dart' as YAML; void main() { YamlBenchmark.main(); JsonBenchmark.main(); CompressedJsonBenchmark.main(); } class YamlBenchmark extends BenchmarkBase { const YamlBenchmark() : super("YAML"); final String text = new File("./test.yaml").readAsStringSync(); static void main() { new YamlBenchmark().report(); } void run() { YAML.loadYaml(text); } } class JsonBenchmark extends BenchmarkBase { const JsonBenchmark() : super("JSON"); final String text = new File("./test.json").readAsStringSync(); static void main() { new JsonBenchmark().report(); } void run() { JSON.decode(text); } } class CompressedJsonBenchmark extends BenchmarkBase { const CompressedJsonBenchmark() : super("Compressed JSON"); final String text = new File("./compressedTest.json").readAsStringSync(); static void main() { new CompressedJsonBenchmark().report(); } void run() { JSON.decode(text); } }
And the results of this benchmark had JSON as a clear winner:
YAML(RunTime): 5755.747126436781 us.
JSON(RunTime): 29.408737335862483 us.
Compressed JSON(RunTime): 24.88459767826704 us.
But then I realized that making the tests like this I made dissapear one of YAML's advantage against not compressed JSON, a smaller file that would take less time to read. So I decided to change the benchmark so the File is read each time and the time it takes to read it is part of the result.
import 'dart:convert'; import 'dart:io'; import 'package:benchmark_harness/benchmark_harness.dart'; import 'package:yaml/yaml.dart' as YAML; void main() { YamlBenchmark.main(); JsonBenchmark.main(); CompressedJsonBenchmark.main(); } class YamlBenchmark extends BenchmarkBase { const YamlBenchmark() : super("YAML"); static void main() { new YamlBenchmark().report(); } void run() { String text = new File("./test.yaml").readAsStringSync(); YAML.loadYaml(text); } } class JsonBenchmark extends BenchmarkBase { const JsonBenchmark() : super("JSON"); static void main() { new JsonBenchmark().report(); } void run() { String text = new File("./test.json").readAsStringSync(); JSON.decode(text); } } class CompressedJsonBenchmark extends BenchmarkBase { const CompressedJsonBenchmark() : super("Compressed JSON"); static void main() { new CompressedJsonBenchmark().report(); } void run() { String text = new File("./compressedTest.json").readAsStringSync(); JSON.decode(text); } }
And here are the results with the modified code:
YAML(RunTime): 6766.891891891892 us.
JSON(RunTime): 240.87679152113694 us.
Compressed JSON(RunTime): 206.1218179944347 us.
De donde sacas el tiempo para currartelo tanto ??? Yo no hago nada en todo el dia y no se me ocurren estas cosas.
ResponderEliminarTampoco me llevo mucho tiempo, entre las pruebas y este post me llevaria 1 hora, y porque la libreria dart_config no estaba actualizada para ser compatible con la versión actual de Dart, si no me hubiese llevado aún menos. Al final todo es ponerse.
ResponderEliminar