Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rrousselGit/0024c507fb6bb0fd74ba5ce199cb440f to your computer and use it in GitHub Desktop.
Save rrousselGit/0024c507fb6bb0fd74ba5ce199cb440f to your computer and use it in GitHub Desktop.
// Import BenchmarkBase class.
import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:state_notifier/state_notifier.dart';
import 'change_notifier.dart';
class MyChangeNotifier extends ChangeNotifier {
int _value;
int get value => _value;
set value(int value) {
_value = value;
notifyListeners();
}
}
// Create a new benchmark by extending BenchmarkBase
class ChangeNotifierBenchmark extends BenchmarkBase {
ChangeNotifierBenchmark(this.listenerCount)
: super('ValueNotifier(listenerCount: $listenerCount');
final int listenerCount;
MyChangeNotifier notifier;
// The benchmark code.
@override
void run() {
for (var i = 0; i < 100; i++) {
notifier.value = i;
}
}
// Not measured setup code executed prior to the benchmark runs.
@override
void setup() {
notifier = MyChangeNotifier();
for (var i = 0; i < listenerCount; i++) {
notifier.addListener(() {});
}
}
// Not measured teardown code executed after the benchmark runs.
@override
void teardown() {
notifier.dispose();
notifier = null;
}
}
class StateNotifierBenchmark extends BenchmarkBase {
StateNotifierBenchmark(this.listenerCount)
: super('StateNotifier(listenerCount: $listenerCount');
final int listenerCount;
MyNotifier notifier;
// The benchmark code.
@override
void run() {
for (var i = 0; i < 100; i++) {
notifier.state = i;
}
}
// Not measured setup code executed prior to the benchmark runs.
@override
void setup() {
notifier = MyNotifier();
for (var i = 0; i < listenerCount; i++) {
notifier.addListener((value) {});
}
}
// Not measured teardown code executed after the benchmark runs.
@override
void teardown() {
notifier.dispose();
notifier = null;
}
}
class MyNotifier extends StateNotifier<int> {
MyNotifier() : super(0);
}
main() {
ChangeNotifierBenchmark(0).report();
ChangeNotifierBenchmark(1).report();
ChangeNotifierBenchmark(2).report();
ChangeNotifierBenchmark(3).report();
ChangeNotifierBenchmark(4).report();
ChangeNotifierBenchmark(5).report();
print('');
StateNotifierBenchmark(0).report();
StateNotifierBenchmark(1).report();
StateNotifierBenchmark(2).report();
StateNotifierBenchmark(3).report();
StateNotifierBenchmark(4).report();
StateNotifierBenchmark(5).report();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment