Reputation: 1508
for my research I'm measuring time what it takes for mobile Client (written in Flutter) to connect to the Server (written in Ktor) through Socket. My application is connecting and disconnecting to the socket 10 times and my results are like this (in milliseconds):
[23, 19, 1, 1, 2, 1, 6, 2, 3, 1]
Can someone explain to me, how is it possible that after 1-2-3 connections, time for the next one takes only a few milliseconds?
Is my testing method wrong or it's some Dart mechanism underneath it?
Functions which connect Client:
void connectionLoop() {
_connectionTimesMultiple().then((connectionTime) {
_connectionTimes.add(connectionTime);
if (_connectionTimes.length < 10) {
connectionLoop();
} else {
print(_connectionTimes);
final numberInList = _connectionTimes.length;
final averageTime =
_connectionTimes.reduce((a, b) => a + b) / numberInList;
print(averageTime);
}
});
}
Future<int> _connectionTimesMultiple() async {
final stopwatch = Stopwatch()..start();
Socket sock = await Socket.connect('10.0.2.2', 8080);
final connectionTime = stopwatch.elapsedMilliseconds;
sock.close();
return connectionTime;
}
And it's triggered on a simple button click.
Upvotes: 0
Views: 465
Reputation: 42353
I don't know a lot about this, but my guess is that it's either JIT compilation (for example your function that's executed when the connection is made may be compiled as part of the first call to it) or some optimisation (when code is called multiple times, it may be possible to optimise it based on the paths it takes and the input values).
That said, if you want to measure performance of an app in Flutter, you should look at profile mode. The performance characteristics could vary significantly between debug mode (which runs in a Dart VM and is optimised for debugging, fast build times, hot reload) and profile/release mode (which is ahead-of-time compiled to native code).
Upvotes: 1