Last active
August 15, 2018 05:11
-
-
Save b-cancel/0a29f5e7005f645becd1db19c6745a16 to your computer and use it in GitHub Desktop.
FLUTTER => Synchronous Operations Taking Up Time In An Asynchronous Function
This file contains 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:async'; | |
import 'package:flutter/material.dart'; | |
/* | |
OUTPUT (one of many) NOTE: how far behind the Loose Timer is, is not predictable either | |
I/flutter (11433): Timer Restarted | |
I/flutter (11433): Timers Started | |
I/flutter (11433): Loose Timer Will Take 588 solid runs of 0:00:00.017000 and an extra run of 0:00:00.004000 | |
Restarted app in 1,822ms. | |
I/flutter (11433): Precise Timer Complete At 2018-07-10 21:07:07.525285 | |
I/flutter (11433): Loose Timer Complete At 2018-07-10 21:07:09.507393 | |
I/flutter (11433): -----Loose Timer is 0:00:01.982108 behind precision timer | |
*/ | |
void main() => runApp(MainApp()); | |
class MainApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
print("Timer Restarted"); | |
AsyncTester test = new AsyncTester(); | |
test.start(); | |
return MaterialApp( | |
home: new Scaffold( | |
body: new Container( | |
padding: EdgeInsets.all(16.0), | |
alignment: Alignment.center, | |
child: new Text( | |
"1. Wait just 10 seconds\n" | |
"2. Then look at the print statements\n\n" | |
"*The precision timer and loose timer stop at different times*\n\n" | |
"even though all I did differently in the loose timer was do some simple operations\n\n" | |
"these simple operations would then have let me read in the duration desired to create a timer" | |
), | |
), | |
), | |
); | |
} | |
} | |
class AsyncTester{ | |
///If you want to test with your own timer intervals | |
///TOTAL TIME = _originalTimer = _timeLeft | |
///TIME BETWEEN LOOSE UPDATED = _timeBetweenUpdates | |
AsyncTester(){ | |
_timeBetweenUpdates = new Duration(milliseconds: 17); //about 60 times per second | |
_originalTime = new Duration(seconds: 10); | |
_timeLeft = new Duration(seconds: 10); | |
_timePassed = Duration.zero; | |
} | |
Duration _timeBetweenUpdates; | |
Duration _originalTime; | |
Duration _timeLeft; | |
Duration _timePassed; | |
//---Test Variables | |
DateTime precisionEnd; | |
DateTime looseEnd; | |
//-------------------------INTERNAL FUNCTIONS------------------------- | |
_startPreciseTimer(Duration waitTime) async{ | |
await Future.delayed(waitTime); | |
//save data for analysis | |
precisionEnd = DateTime.now(); | |
print("Precise Timer Complete At " + precisionEnd.toString()); | |
_finalPrint(); | |
} | |
_looseTimerStart() async{ | |
//calculate how many solid run of our chosen interval we can do | |
int solidRuns = (_timeLeft.inMicroseconds / _timeBetweenUpdates.inMicroseconds).truncate(); | |
Duration lastRun = _originalTime - (_timeBetweenUpdates * solidRuns); | |
print("Loose Timer Will Take $solidRuns solid runs of " + _timeBetweenUpdates.toString() + " and an extra run of " + lastRun.toString()); | |
for(int i=(solidRuns+1); i>0; i--){ | |
if(i==1){ //the only non solid run | |
//the wait | |
await Future.delayed(lastRun); | |
//the subtraction | |
_timeLeft -= lastRun; | |
_timePassed = _originalTime - _timeLeft; | |
} | |
else{ | |
//the wait | |
await Future.delayed(_timeBetweenUpdates); | |
//the subtraction | |
_timeLeft -= _timeBetweenUpdates; | |
_timePassed = _originalTime - _timeLeft; | |
} | |
} | |
//save data for analysis | |
looseEnd = DateTime.now(); | |
print("Loose Timer Complete At " + looseEnd.toString()); | |
_finalPrint(); | |
} | |
_finalPrint(){ | |
if(precisionEnd != null && looseEnd != null){ | |
print("-----Loose Timer is " + looseEnd.difference(precisionEnd).toString() + " behind precision timer"); | |
} | |
} | |
//-------------------------SINGLE ACTION FUNCTIONS------------------------- | |
start(){ | |
print("Timers Started"); | |
_looseTimerStart(); | |
_startPreciseTimer(_timeLeft); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment