Skip to content

Instantly share code, notes, and snippets.

@ali2236
Created October 13, 2020 10:04
Show Gist options
  • Save ali2236/a03bd6cdc7326d08963e1914ef4ba4aa to your computer and use it in GitHub Desktop.
Save ali2236/a03bd6cdc7326d08963e1914ef4ba4aa to your computer and use it in GitHub Desktop.
fibonacci dart ffi
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}');
}
@ali2236
Copy link
Author

ali2236 commented Oct 13, 2020

Results on my comupter:
n = 45
Native C: 1836311903 in 0:00:07.551995
DartVM: 1836311903 in 0:00:05.751678

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment