Created
September 22, 2021 13:58
-
-
Save Midi12/861e0b9a5f8a2300c1264d862ef70ccd to your computer and use it in GitHub Desktop.
Benchmark ffi calls
This file contains hidden or 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:core'; | |
import 'dart:ffi'; | |
final _libc = DynamicLibrary.open('libc.so.6'); | |
typedef abs_t = Int32 Function(Int32); | |
typedef abs_d = int Function(int); | |
final _abs = _libc.lookupFunction<abs_t, abs_d>('abs'); | |
void main() { | |
print('_libc $_libc'); | |
print('_abs $_abs'); | |
int a = -1337; | |
int b = 0; | |
int n = 10000000; | |
int ms = 0; | |
int us = 0; | |
print('start benchmark ..'); | |
Stopwatch sw = Stopwatch()..start(); | |
for (int i = 1; i < n; i++) { | |
sw.reset(); | |
b = _abs(a); | |
sw.stop(); | |
us += sw.elapsedMicroseconds; | |
ms += sw.elapsedMilliseconds; | |
} | |
print('$n iterations took ${ms}ms (${us}us)'); | |
print('average : ${ms / n}ms (${us / n}us)'); | |
print('a = $a'); | |
print('b = $b'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment