2018年8月5日日曜日

Visualizing Operations on Java Serial/Parallel Streams

Let's examine the operations of streams installed in Java SE8 or later. Again, lambda expression is essential. You can use the CPU performance monitor or peek command to observe the flow of processing (pipeline). However, here we want to observe at a user program level in a more friendly way. There is a good program (StreamTurtle.java) in the examples published at the following URL made by Professors Hideki Tsuiki and Taeko Ariga. 




The above program was created with reference to it.
  • Line 16: A stream composed of integers 1 to 8 is generated.
  • Line 17: Replace each integer of the stream with an object of FPTurtle class which means turtle. Depending on the integer, the (x, y) coordinate and orientation of the turtle are given.
  • Line 21: Each turtle m of the stream draws a polygon with a red color. (The draw method draws a dodecagon with the specified color.) Leave it in the stream.
  • Line 22: Each turtle m of the stream moves slightly to the right and then draws a polygon in green. Leave it in the stream as it is.
  • Line 23: Each turtle m of the stream moves slightly to the right and then draws a polygon with blue color. Leave it in the stream as it is.
  • Line 24: This is a stream end operation. After making each turtle of the stream black, advance forward by 100 steps and stop.



The above figure shows the execution status of this program. Compared to the source list, is something wrong? is something strange? In line 21, all 8 turrets should be red and draw a polygon. After that, in line 22 all turtles draw a green polygon this time. It seems to proceed like that. However, actual execution results are not. As you can see from this execution result, the stream of line 17 begins to run when the aggregation of stream of line 24 starts. Continuously, it flows downward like a pipeline.

Now, let's put the following method call in line 20 of the above list.
          .parallel()
The result of the execution is shown in the figure below. This parallel () is for parallel processing of streams. It was quite different from the previous run. It seems that any four turtles are running at the same time. Exactly, the PC used for this execution has 4 cores. It seems that parallel execution has been done accordingly.






Next, let's run it on another PC with 8 cores. The result is shown below. This time, surely, processing by 8 turtles is always carried out.




The execution time (elapsed time) is also shorter according to the number of used cores as follows. However, since the other programs are running on the PC and the usage of the memory varies, the execution time will fluctuate to some extent each time it is executed.


0 件のコメント:

コメントを投稿