-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpt4o.dart
71 lines (59 loc) · 1.93 KB
/
gpt4o.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import 'dart:async';
import 'dart:isolate';
import 'dart:math';
class CpuBenchmark {
final int iterations = 1000000; // Default iterations for consistent results
late int singleThreadScore;
late int multiThreadScore;
Future<void> runBenchmark() async {
singleThreadScore = await _runSingleThreadTest();
multiThreadScore = await _runMultiThreadTest();
var totalScore = singleThreadScore + multiThreadScore;
print('Single-thread score: ${_formatBytes(singleThreadScore)}');
print('Multi-thread score: ${_formatBytes(multiThreadScore)}');
print('Total score: ${_formatBytes(totalScore)}');
}
Future<int> _runSingleThreadTest() async {
var stopwatch = Stopwatch()..start();
await _compute();
stopwatch.stop();
return iterations * 1000 ~/ stopwatch.elapsedMilliseconds;
}
Future<void> _compute() async {
for (int i = 0; i < iterations; i++) {
var calc = sqrt(i) * pow(i, 2);
}
}
Future<int> _runMultiThreadTest() async {
var stopwatch = Stopwatch()..start();
var isolates = List.generate(4, (_) => _spawnIsolate());
await Future.wait(isolates);
stopwatch.stop();
return (iterations * 4 * 1000) ~/ stopwatch.elapsedMilliseconds;
}
Future<void> _isolateEntry(SendPort sendPort) async {
for (int i = 0; i < iterations; i++) {
var calc = sqrt(i) * pow(i, 2);
}
sendPort.send(null);
}
Future<void> _spawnIsolate() async {
var receivePort = ReceivePort();
await Isolate.spawn(_isolateEntry, receivePort.sendPort);
await receivePort.first;
}
String _formatBytes(int bytes) {
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
int unitIndex = 0;
double size = bytes.toDouble();
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024;
unitIndex++;
}
return '${size.toStringAsFixed(2)} ${units[unitIndex]}';
}
}
void main() async {
var benchmark = CpuBenchmark();
await benchmark.runBenchmark();
}