|
| 1 | +// ignore_for_file: avoid_print |
| 2 | +import 'dart:convert'; |
| 3 | +import 'dart:typed_data'; |
| 4 | + |
| 5 | +import 'package:dartoos/dartoos.dart'; |
| 6 | +import 'package:dartoos/src/encoding/base64/base64_enc.dart'; |
| 7 | + |
| 8 | +import '../utils/perf_gain.dart'; |
| 9 | + |
| 10 | +/// Dartoos base64-encoder/decoder vs. Dart's built-in base64-encoder/decoder. |
| 11 | +/// |
| 12 | +/// Running: |
| 13 | +/// ```shell |
| 14 | +/// dart run example/encoding/base64_benchmark.dart |
| 15 | +/// ``` |
| 16 | +/// |
| 17 | +/// or |
| 18 | +/// |
| 19 | +/// Compile to 'jit'. |
| 20 | +/// ```shell |
| 21 | +/// dart compile jit-snapshot example/encoding/base64_benchmark.dart |
| 22 | +/// dart run /example/encoding/base64_benchmark.jit |
| 23 | +/// ``` |
| 24 | +void main() { |
| 25 | + print("Dartoos base64 vs. Dart's built-in base64..."); |
| 26 | + |
| 27 | + const len = 50000000; |
| 28 | + const alphabet = |
| 29 | + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
| 30 | + final bytes = BytesOf.text(Rand(len, alphabet)).value; |
| 31 | + print('\nLength of the data to be encoded: ${bytes.length} bytes.'); |
| 32 | + |
| 33 | + print('\n--- Encoding elapsed times ---'); |
| 34 | + final watch = Stopwatch()..start(); |
| 35 | + final dartEnc = base64Encode(bytes); |
| 36 | + final dartEncTime = watch.elapsedMicroseconds / 1000; |
| 37 | + print('Dart base64-encoding......: $dartEncTime milliseconds'); |
| 38 | + watch.stop(); |
| 39 | + watch.reset(); |
| 40 | + |
| 41 | + watch.start(); |
| 42 | + const dartoosBase64Enc = Base64Enc(); |
| 43 | + final dartoosEnc = dartoosBase64Enc(bytes); |
| 44 | + final dartoosEncTime = watch.elapsedMicroseconds / 1000; |
| 45 | + print('Dartoos base64-encoding...: $dartoosEncTime milliseconds'); |
| 46 | + watch.stop(); |
| 47 | + watch.reset(); |
| 48 | + const perf = PerfGain(); |
| 49 | + print('Encoding Performance......: ${perf(dartEncTime, dartoosEncTime)}'); |
| 50 | + |
| 51 | + print('\n--- Decoding elapsed times ---'); |
| 52 | + watch.start(); |
| 53 | + final dartDec = base64Decode(dartEnc); |
| 54 | + final dartDecTime = watch.elapsedMicroseconds / 1000; |
| 55 | + print('Dart base64-decoding......: $dartDecTime milliseconds'); |
| 56 | + watch.stop(); |
| 57 | + watch.reset(); |
| 58 | + |
| 59 | + watch.start(); |
| 60 | + const dartoosDecoder = Base64Dec(); |
| 61 | + final dartoosDec = dartoosDecoder(dartoosEnc); |
| 62 | + final dartoosDecTime = watch.elapsedMicroseconds / 1000; |
| 63 | + print('Dartoos base64-decoding...: $dartoosDecTime milliseconds'); |
| 64 | + watch.stop(); |
| 65 | + watch.reset(); |
| 66 | + print('Decoding Performance......: ${perf(dartDecTime, dartoosDecTime)}'); |
| 67 | + |
| 68 | + print('\n--- Encoding/decoding results comparison ---'); |
| 69 | + print('dartEncoded == dartoosEncoded? ${dartEnc == dartoosEnc}'); |
| 70 | + print('dartDecoded == dartoosDecoded? ${bytesEqual(dartDec, dartoosDec)}'); |
| 71 | +} |
| 72 | + |
| 73 | +/// Checks for the equality between [first] and [second]. |
| 74 | +bool bytesEqual(Uint8List first, Uint8List second) { |
| 75 | + if (first.length != second.length) return false; |
| 76 | + final length = first.length; |
| 77 | + int i = 0; |
| 78 | + while ((i < length) && (first[i] == second[i])) { |
| 79 | + ++i; |
| 80 | + } |
| 81 | + return i == length; |
| 82 | +} |
| 83 | + |
| 84 | +/// Returns a formatted string describing the performance gain ('+', positive |
| 85 | +/// values) or loss ('-', negative values). |
| 86 | +/// |
| 87 | +/// Examples: +12% for a performance gain; -5.5% for a performance loss. |
| 88 | +// String perfGain(double dartTime, double dartoosTime) { |
| 89 | +// final perc = ((dartTime / dartoosTime) * 100) - 100; |
| 90 | +// final sign = perc > 0 ? '+' : ''; |
| 91 | +// return "$sign${perc.toStringAsFixed(2)}% ('+' gain; '-' loss)"; |
| 92 | +// } |
0 commit comments