Skip to content

Instantly share code, notes, and snippets.

@alifgiant
Last active April 30, 2023 09:11
Show Gist options
  • Save alifgiant/42bb081a8be4873c96c09c8dd9bad9eb to your computer and use it in GitHub Desktop.
Save alifgiant/42bb081a8be4873c96c09c8dd9bad9eb to your computer and use it in GitHub Desktop.
Testing Isolate Perf
import 'dart:convert';
import 'dart:isolate';
void main() async {
const jsonStr = '{'
'"name": "alif",'
'"age": 26,'
'"gender": "male",'
'"office": "work from home"'
'}';
const testN = 100;
Future asyncRun<Q>(Function(Q) function, Q message) async {
function(message);
}
final stopwatchA = Stopwatch()..start();
for (var i = 0; i < testN; i++) {
jsonDecode(jsonStr);
}
print('$testN sync run : ${stopwatchA.elapsed}');
final stopwatchB = Stopwatch()..start();
await Future.wait(
[for (var i = 0; i < testN; i++) asyncRun(jsonDecode, jsonStr)],
);
print('$testN async run : ${stopwatchB.elapsed}');
final stopwatchC = Stopwatch()..start();
await Future.wait(
[for (var i = 0; i < testN; i++) Isolate.spawn(jsonDecode, jsonStr)],
);
print('$testN isolate.spawn: ${stopwatchC.elapsed}');
}
@alifgiant
Copy link
Author

alifgiant commented Jan 5, 2022

image

@nikhil-RGB
Copy link

nikhil-RGB commented Mar 19, 2023

Hello, I was building an app for viewing cellular automaton and am a fairly new flutter dev, I've worked with JAVA before.
Was writing code to calculate and update the board every generation.
Speed is the only factor here.
I assume I should you asynchronous execution rather than creating a seperate isolate for it?
I need to make the code stop for 50 ms evey computation cycle to ensure the UI doesnt get blocked. Will this not be a problem if I use Isolates?
The Project's Repo

@alifgiant
Copy link
Author

alifgiant commented Mar 20, 2023

hi @nikhil-RGB , what i learned from my experiment on using isolates:

  1. secondary isolate is slower than the main isolate in executing codes.
  2. creating new isolate for every task is slower than using only small number of isolate.
  3. compute(..) function is creating a new isolate every time you called it.
  4. Async execution is actually still run on main isolate (so it's faster), but it could postpone task on every await to unblock UI.

so my recommendation for your case is:
if the task that you need to run for every 50ms is short live, like simple addition or boolean check, you should definitely use async/await only. but, if the task is long running like doing heavy computation such as image processing you need to use Isolate, but try to maintain only small number of it.

@nikhil-RGB
Copy link

hi @nikhil-RGB , what i learned from my experiment on using isolates:

  1. secondary isolate is slower than the main isolate in executing codes.
  2. creating new isolate for every task is slower than using only small number of isolate.
  3. compute(..) function is creating a new isolate every time you called it.
  4. Async execution is actually still run on main isolate (so it's faster), but it could postpone task on every await to unblock UI.

so my recommendation for your case is: if the task that you need to run for every 50ms is short live, like simple addition or boolean check, you should definitely use async/await only. but, if the task is long running like doing heavy computation such as image processing you need to use Isolate, but try to maintain only small number of it.

Yes, thank you for the response!

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