Serial and Parallel Scala Programming

1. The concept of serial and parallel

The serial (Sequential) and parallel (Parallel) of the program refer to the way the tasks in the program are executed.

Serial means that tasks are executed one after another in sequence, and the next task will not be executed until the previous task is completed. In this way, each task has to wait for the completion of the previous task before it can start executing, so the execution time of tasks is cumulative.

Parallelism refers to the execution of multiple tasks at the same time. Multiple tasks can be executed concurrently on different processor cores, threads or computing nodes, independent of each other, and will not block each other. In this way, multiple tasks can be executed in parallel, thereby improving the overall execution efficiency.

The choice between serial and parallel usually depends on the characteristics of the task and the demand on execution time.

Here is an example to illustrate the difference between serial and parallel:

Suppose there are three tasks A, B, and C that take 2, 3, and 4 seconds to execute, respectively.

In the case of serial execution, tasks A, B, and C are completed in sequence, and the total time required is 2 seconds + 3 seconds + 4 seconds = 9 seconds.

In the case of parallel execution, only tasks A, B, and C need to be executed at the same time, and the total time required is the execution time of the longest task, which is 4 seconds.

As you can see, in this example, parallel execution is more efficient than serial execution.

It should be noted that parallel execution may have challenges such as resource competition and synchronization issues that require additional processing. Therefore, when choosing serial or parallel execution, it is necessary to comprehensively consider the characteristics of the task, system resources and requirements, and evaluate various factors to make an appropriate decision.

2. Serial and parallel programming

package test.scala

object ParTest1 {
    
    

  

  def main(args: Array[String]): Unit = {
    
    
    println("*" * 100)
    // 串行
    for (i <- Range(0, 10)){
    
    
      println("串行", i)
    }

    println("#" * 100)
    // 并行
    scala.collection.parallel.immutable.ParRange(0, 10, 1, false).foreach(i => {
    
    
      println("并行", i)
    })

  }
}

Console printout:

****************************************************************************************************
(串行,0)
(串行,1)
(串行,2)
(串行,3)
(串行,4)
(串行,5)
(串行,6)
(串行,7)
(串行,8)
(串行,9)
####################################################################################################
(并行,0)
(并行,1)
(并行,8)
(并行,5)
(并行,3)
(并行,4)
(并行,6)
(并行,9)
(并行,2)
(并行,7)

It can be seen that parallelism is out of order.
Verify again, whether parallelism can be executed at the same time

package test.scala

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

object ParTest1 {
    
    

  def getCurrentTime() : String = {
    
    
    // 获取当前时间
    val currentTime = LocalDateTime.now()
    // 定义时间格式
    val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
    // 格式化并打印当前时间
    currentTime.format(formatter)
  }

  def serRunTest() : Unit = {
    
    
    // 串行
    for (i <- Range(0, 10)) {
    
    
      Thread.sleep(1000)
      println("串行", i, getCurrentTime())
    }
  }


  def parRunTest(): Unit = {
    
    
    // 并行
    scala.collection.parallel.immutable.ParRange(0, 10, 1, false).foreach(i => {
    
    
      Thread.sleep(1000)
      println("并行", i, getCurrentTime())
    })
  }

  def main(args: Array[String]): Unit = {
    
    
    println("*" * 100)
    serRunTest()
    println("#" * 100)
    parRunTest()
  }
}

Console run output:

****************************************************************************************************
(串行,0,2023-07-16 22:05:28)
(串行,1,2023-07-16 22:05:29)
(串行,2,2023-07-16 22:05:30)
(串行,3,2023-07-16 22:05:31)
(串行,4,2023-07-16 22:05:32)
(串行,5,2023-07-16 22:05:33)
(串行,6,2023-07-16 22:05:34)
(串行,7,2023-07-16 22:05:35)
(串行,8,2023-07-16 22:05:36)
(串行,9,2023-07-16 22:05:37)
####################################################################################################
(并行,4,2023-07-16 22:05:39)
(并行,1,2023-07-16 22:05:39)
(并行,5,2023-07-16 22:05:39)
(并行,2,2023-07-16 22:05:39)
(并行,9,2023-07-16 22:05:39)
(并行,3,2023-07-16 22:05:39)
(并行,8,2023-07-16 22:05:39)
(并行,7,2023-07-16 22:05:39)
(并行,0,2023-07-16 22:05:39)
(并行,6,2023-07-16 22:05:39)

It can be seen that serial is executed one by one, and parallel can be executed at the same time.

Guess you like

Origin blog.csdn.net/programmer589/article/details/131755908