Closed
Description
Hello everyone! Maybe I'm doing something wrong here, but I'm experiencing a pretty heavy performance hit with double.remainder()
on ARM64 (M3) in Dart 3.3.3 on macOS.
As per the documentation, a.remainder(b)
is mathematically equivalent to:
double remainder(final double a, final num b) => a - (a ~/ b) * b;
But when I compare the two, the custom implementation is somewhere between one and two orders of magnitude faster:
class RemainderBenchmark extends BenchmarkBase {
const RemainderBenchmark() : super('Remainder');
@override
void run() {
for (double i = -500; i <= 500; i += 0.75) {
for (double j = -500; j <= 500; j += 0.75) {
final _ = i.remainder(j);
}
}
}
}
class RemainderCustomBenchmark extends BenchmarkBase {
const RemainderCustomBenchmark() : super('RemainderCustom');
@override
void run() {
for (double i = -500; i <= 500; i += 0.75) {
for (double j = -500; j <= 500; j += 0.75) {
final _ = remainder(i, j);
}
}
}
}
AOT (dart compile exe
):
RemainderCustom(RunTime): 38108.96153846154 us.
Remainder(RunTime): 478124.0 us. <- About 13x slower!
JIT (dart run
):
RemainderCustom(RunTime): 9825.791469194313 us.
Remainder(RunTime): 507507.0 us. <- About 52x slower!
Any ideas why this is the case?