Once in the comments, VladD shared information that one of his colleagues, a network programmer, had moved from multi-threaded to asynchronous network programming. I would like to use the example of a specific task to find out how much asynchrony will gain from multithreading.
Task: take one of the simple network protocols - RFB . We need to simultaneously connect to 10 000 servers with RFB on board, and find out the version of RFB .
How to implement this multithreadedly - I know, but how to implement it asynchronously? And how much, in this problem, asynchrony will win? The implementation of RFB itself is not needed, we need an example of executing 10 000 simultaneous asynchronous requests.
Tested 3 code variants:
Multithreaded (changed the breakdown of the list to several - on a thread-safe queue to level the chances)
Asynchronous
Asynchronous (
Throttlingpattern)
Results (checking 300 000 IP addresses):
- Multi-threaded: 3 minutes 18 seconds
- Asynchronous: 1 minute 27 seconds
- Asynchronous (
Throttlingpattern): when it has exceeded 6 minutes, I closed the program and did not measure further. Speed can be achieved only by using the level of parallelism - the size of the entire list, but then the meaning of the very use of the pattern is lost. Those. the implementation fromandreycha, if you use the level of parallelism less than the size of the list - it works longer than even the multi-threaded version. Perhaps this is justмояmistake, orreycha error.
Conclusion:
- Standard asynchronous implementation works more than 2 times faster than multi-threaded.