Performance Scala ProcessBuilder管道运行缓慢

Performance Scala ProcessBuilder管道运行缓慢,performance,scala,process,pipe,Performance,Scala,Process,Pipe,通过示例更容易显示: $ dd if=/dev/urandom of=test.raw bs=1M count=100 100+0 records in 100+0 records out 104857600 bytes (105 MB) copied, 6.50489 s, 16.1 MB/s $ gzip test.raw $ time gunzip -k test.raw.gz 0.54s user 4MB memory 99% cpu 0.612 total - gunz

通过示例更容易显示:

$ dd if=/dev/urandom of=test.raw bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 6.50489 s, 16.1 MB/s

$ gzip test.raw

$ time gunzip -k test.raw.gz
0.54s user   4MB memory   99% cpu   0.612 total - gunzip -k test.raw.gz 

$ time cat test.raw.gz | gunzip -c > /dev/null
0.00s user   4MB memory   11% cpu   0.500 total - cat test.raw.gz
0.48s user   4MB memory   99% cpu   0.499 total - gunzip -c > /dev/null
在Scala中,没有管道也可以正常工作,但是有了管道

scala> import java.io.File; val t2,t1 = 0.0

scala> t1 = System.nanoTime; "gunzip -k test.raw.gz".!; t2 = System.nanoTime; "rm -f test.raw".!; println((t2-t1)/1e9)
0.547709904

scala> t1 = System.nanoTime; ("cat test.raw.gz" #| "gunzip -c" #> new File("/dev/null")).!; t2 = System.nanoTime; "rm -f test.raw".!; println((t2-t1)/1e9)
2.806807235
因此,在这里使用Scala的ProcessBuilder管道似乎会将速度降低5倍以上。而我天真地认为它将在后端使用Unix命名管道来提供几乎相同的性能

那么(a):为什么速度慢?(b) :我怎样才能使它更快


谢谢

尝试
strace-r-f-efilescala
记录系统调用。可能scala没有直接在
cat
gunzip
之间创建管道,或者其他什么
-efile
可能显示得不够多,因此可能
-eclone、execve、open、close、dup、dup2