Last active
March 30, 2021 12:26
-
-
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().
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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