"""每个输入并发执行两个步骤;独立 Rust 使用相同 Tokio,Go 使用原生 goroutine。""" def source(language): if language == 'dever': return '''noop() () {} rule pause(value: Int) () | value | do | | 0 | dever.time.sleep(1) | | other | noop() | flow item(value: Int) (out: Int) | step | kind | do | | - | rule | pause(value % 1000000) | | out | calc | value | flow pair(value: Int) (out: Int) | step | kind | parallel | do | | left | flow | read | item(value) | | right | flow | read | item(value + 1) | | out | calc | - | left + right | loop measure(n: Int) (out: Int) | each value in range(0, n) | result | | out | sum pair(value) | cmd bench = app.measure ''' if language == 'rust': return '''async fn pause(value: i64) -> Result<(), &'static str> { if value == 0 { let millis = u64::try_from(1i64).map_err(|_| "sleep duration must be nonnegative")?; let deadline = tokio::time::Instant::now().checked_add(std::time::Duration::from_millis(millis)).ok_or("sleep deadline is out of range")?; tokio::time::sleep_until(deadline).await; } Ok(()) } async fn item(value: i64) -> Result { pause(value % 1000000).await?; Ok(value) } async fn pair(value: i64) -> Result { let left = value; let right = value.checked_add(1).expect("argument overflow"); let (left, right) = tokio::try_join!(biased; item(left), item(right))?; Ok(left.checked_add(right).expect("pair overflow")) } async fn measure(n: i64) -> Result { let mut total = 0i64; for value in 0..n { total = total.checked_add(pair(value).await?).expect("sum overflow"); } Ok(total) } fn main() { let n: i64 = std::env::args().nth(1).unwrap().parse().unwrap(); let runtime = tokio::runtime::Builder::new_current_thread().enable_time().build().unwrap(); println!("{}", runtime.block_on(measure(n)).unwrap()); } ''' return '''package main import ("fmt"; "os"; "strconv"; "sync"; "time") func add(a, b int64) int64 { out := a + b if (b > 0 && out < a) || (b < 0 && out > a) { panic("integer overflow") } return out } func item(value int64) int64 { if value % 1000000 == 0 { time.Sleep(time.Millisecond) } return value } func pair(value int64) int64 { left, right := value, add(value, 1) var a, b int64 var group sync.WaitGroup group.Add(2) go func() { defer group.Done(); a = item(left) }() go func() { defer group.Done(); b = item(right) }() group.Wait() return add(a, b) } func main() { n, err := strconv.ParseInt(os.Args[1], 10, 64); if err != nil { panic(err) } total := int64(0) for value := int64(0); value < n; value++ { total = add(total, pair(value)) } fmt.Println(total) } '''