55 lines
2.0 KiB
Python
55 lines
2.0 KiB
Python
"""顺序调用直接 future,百万次调用中实际挂起一次;对端不共享 Dever 运行库。"""
|
|
|
|
|
|
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 |
|
|
loop measure(n: Int) (out: Int)
|
|
| each value in range(0, n) | result |
|
|
| out | sum item(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<i64, &'static str> { pause(value % 1000000).await?; Ok(value) }
|
|
async fn measure(n: i64) -> Result<i64, &'static str> {
|
|
let mut total = 0i64;
|
|
for value in 0..n { total = total.checked_add(item(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"; "time")
|
|
func pause(value int64) { if value == 0 { time.Sleep(time.Millisecond) } }
|
|
func item(value int64) int64 { pause(value % 1000000); return value }
|
|
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++ {
|
|
next := total + item(value)
|
|
if next < total { panic("sum overflow") }; total = next
|
|
}
|
|
fmt.Println(total)
|
|
}
|
|
'''
|