为什么雨燕慢比C在该图像处理试验100次? [英] Why Swift is 100 times slower than C in this image processing test?

查看:139
本文介绍了为什么雨燕慢比C在该图像处理试验100次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像许多其他开发商我一直对苹果新雨燕的语言非常兴奋。苹果公司声称其速度比目标C更快,可以用来编写操作系统。从我了解到,到目前为止,它是一个静态类型语言,能有超过的确​​切数据类型$ P ​​$ pcisely控制(如整数长度)。因此,它看起来像具有良好潜力的操控性能的关键任务,如图像处理,对吧?

Like many other developers I have been very excited about the new Swift language from Apple. Apple has claimed its speed is faster than Objective C and can be used to write operating system. And from what I learned so far, it's a static typed language and able to have precisely control over the exact data type (like integer length). So it does look like having good potential handling performance critical tasks, like image processing, right?

这就是我想在我进行了快速测试。结果真让我吃惊。

That's what I thought before I carried out a quick test. The result really surprised me.

下面是在C简单code片断:

Here is a simple code snippet in C:

test.c的:

#include <stdio.h>
#include <stdint.h>
#include <string.h>

uint8_t pixels[640*480];
uint8_t alpha[640*480];
uint8_t blended[640*480];

void blend(uint8_t* px, uint8_t* al, uint8_t* result, int size)
{
    for(int i=0; i<size; i++) {
        result[i] = (uint8_t)(((uint16_t)px[i]) *al[i] /255);
    }
}

int main(void)
{
    memset(pixels, 128, 640*480);
    memset(alpha, 128, 640*480);
    memset(blended, 255, 640*480);

    // Test 10 frames
    for(int i=0; i<10; i++) {
        blend(pixels, alpha, blended, 640*480);
    }

    return 0;
}

我用下面的命令编译它在我​​的Macbook Air 2011:

I compiled it on my Macbook Air 2011 with the following command:

clang -O3 test.c -o test

10帧处理时间约为0.01秒。换句话说,它需要的C code 1毫秒处理一帧:

The 10 frame processing time is about 0.01s. In other words, it takes the C code 1ms to process one frame:

$ time ./test
real    0m0.010s
user    0m0.006s
sys     0m0.003s

然后我有同样的code的斯威夫特版本:

Then I have a Swift version of the same code:

test.swift:

test.swift:

let pixels = UInt8[](count: 640*480, repeatedValue: 128)
let alpha = UInt8[](count: 640*480, repeatedValue: 128)
let blended = UInt8[](count: 640*480, repeatedValue: 255)

func blend(px: UInt8[], al: UInt8[], result: UInt8[], size: Int)
{
    for(var i=0; i<size; i++) {
        var b = (UInt16)(px[i]) * (UInt16)(al[i])
        result[i] = (UInt8)(b/255)
    }
}

for i in 0..10 {
    blend(pixels, alpha, blended, 640*480)
}

构建命令行是:

xcrun swift -O3 test.swift -o test

下面我用同样的 O3 级优化标志作出比较公平的希望。然而,所得到的速度为100时慢

Here I use the same O3 level optimization flag to make the comparison hopefully fair. However, the resulting speed is 100 time slower:

$ time ./test

real    0m1.172s
user    0m1.146s
sys     0m0.006s

在换句话说,它需要夫特〜120毫秒到处理一个帧这需要Ç仅有1毫秒。

In other words, it takes Swift ~120ms to processing one frame which takes C just 1 ms.

发生了什么事?

$ gcc -v
Configured with: --prefix=/Applications/Xcode6-Beta.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.34.4) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.2.0
Thread model: posix

更新:更多结果与不同的运行迭代:

下面是不同数量的框架,即,结果主循环数从10改至其他号码。注意现在我得到更快的C code时间,而斯威夫特时间没有太大变化(缓存中的热点?):

Update: more results with different running iterations:

Here are the result for different number of "frames", i.e. change the main for loop number from 10 to other numbers. Note now I am getting even faster C code time (cache hot?), while the Swift time doesn't change too much:

             C Time (s)      Swift Time (s)
  1 frame:     0.005            0.130
 10 frames(*): 0.006            1.196
 20 frames:    0.008            2.397
100 frames:    0.024           11.668

更新:`-Ofast`帮助

使用 -Ofast 按@mweathers建议,雨燕速度上升到合理的范围。

Update: `-Ofast` helps

With -Ofast suggested by @mweathers, the Swift speed goes up to reasonable range.

在我的笔记本电脑斯威夫特版本 -Ofast 获得0.013s 10帧和0.048s为100帧,接近到C性能的一半。

On my laptop the Swift version with -Ofast gets 0.013s for 10 frames and 0.048s for 100 frames, close to half of the C performance.

推荐答案

与楼:

xcrun迅速-Ofast test.swift -o测试

我得到的时间:

real    0m0.052s
user    0m0.009s
sys 0m0.005s

这篇关于为什么雨燕慢比C在该图像处理试验100次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆