Created
October 13, 2020 10:04
-
-
Save ali2236/a03bd6cdc7326d08963e1914ef4ba4aa to your computer and use it in GitHub Desktop.
fibonacci dart ffi
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:ffi'; | |
// long fibonacci(int n) | |
typedef NativeFibonacci = Int64 Function(Int64 n); | |
typedef DartFibonacci = int Function(int n); | |
class Fibonacci { | |
static final dll = DynamicLibrary.open('fibonacci.dll'); | |
final cFibo = dll.lookupFunction<NativeFibonacci, DartFibonacci>('fibonacci'); | |
} | |
int fibo(int n) { | |
if (n <= 1) return 1; | |
return fibo(n - 1) + fibo(n - 2); | |
} | |
void main(List<String> arguments) { | |
var fiboDll = Fibonacci(); | |
var watch = Stopwatch()..start(); | |
int n = 45; | |
print('n = $n'); | |
print('Native C: ${fiboDll.cFibo(n)} in ${watch.elapsed}'); | |
watch..reset()..start(); | |
print('DartVM: ${fibo(n)} in ${watch.elapsed}'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results on my comupter:
n = 45
Native C: 1836311903 in 0:00:07.551995
DartVM: 1836311903 in 0:00:05.751678