Skip to content

Instantly share code, notes, and snippets.

@Sp4Rx
Last active March 30, 2021 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sp4Rx/b6e809ceaf52e80ee7e78b3c940e5d5f to your computer and use it in GitHub Desktop.
Save Sp4Rx/b6e809ceaf52e80ee7e78b3c940e5d5f to your computer and use it in GitHub Desktop.
Benchmarking for to test -- operator x ~/ y is more efficient than (x / y).toInt().
import 'dart:typed_data';
void main() {
const t = 99999999999999999999.0; //A big number
const step = 18871268712.0; //Random number
const max = 999999;
//Initialise time for calculating benchmark.
var initialTime = DateTime.now().millisecondsSinceEpoch;
for (int i = 0; i < max; i++) {
t ~/ step;
}
print("t ~/ step = ${t ~/ step}");
//Print time taken
print("Time taken: ${DateTime.now().millisecondsSinceEpoch - initialTime} ms");
//--------------------------------------------------------------------------
//Init time again
initialTime = DateTime.now().millisecondsSinceEpoch;
for (int i = 0; i < max; i++) {
(t / step).toInt();
}
print("(t / step).toInt() = ${(t / step).toInt()}");
//Print time taken
print("Time taken: ${DateTime.now().millisecondsSinceEpoch - initialTime} ms");
//--------------------------------------------------------------------------
//Init time again
initialTime = DateTime.now().millisecondsSinceEpoch;
for (int i = 0; i < max; i++) {
(t / step).truncate();
}
print("(t / step).truncate() = ${(t / step).truncate()}");
//Print time taken
print("Time taken: ${DateTime.now().millisecondsSinceEpoch - initialTime} ms");
//--------------------------------------------------------------------------
//Init time again
initialTime = DateTime.now().millisecondsSinceEpoch;
for (int i = 0; i < max; i++) {
(Float32x4.splat(t) / Float32x4.splat(step)).x.truncate();
}
print(
"(Float32x4.splat(t) / Float32x4.splat(step)).x.truncate() = ${(Float32x4.splat(t) / Float32x4.splat(step)).x.truncate()}");
//Print time taken
print("Time taken: ${DateTime.now().millisecondsSinceEpoch - initialTime} ms");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment