swift 性能弱爆了

苹果刚推出了swift语言,我们对其进行了性能测试,测试环境如及其测试方法下  :

硬件环境:mac book pro late 2013

测试方法:测量排序使用的时间

编程语言:swift,c,go语言


测试结果

编程   语言循环次数时间总  消耗
c1亿最快31秒
go1亿48秒
swift20万88秒x500


结论:

      c语言的性能还是最强悍,swift语言的性能和苹果宣称的还是有很大差距的



/*swift语言的代码  */

var t1 = 0

time(&t1)

forvar xx = 0;xx < 200000;++xx{            //注意循环次数

    var arry = [7,10,19,25,12,17,21,30,48,100,-1000,30,59,43,21,34,56,87]

    sort(arry){$0<$1}                                      //swift语言本身提供的排序方法,应该用的是快速排序

}

var t2 = 0

println("时间消耗 \(time(&t2)-t1)")


结果:

时间消耗 88

Program ended with exit code: 0


/*C语言的代码,这里排序方式采用归并排序*/

#include <stdlib.h>

#include <stdio.h>

#include <time.h>





int myArry[100];

void merge(int *arry,int left,int center,int right){

int postleft  =left;

int postright =center+1;

    for (int i = left; i<= right; i++) {

        if (postleft <= center && postright <= right) {

            if (arry[postleft] > arry[postright]) {

                myArry[i]   = arry[postright++];

            }else

            {

                myArry[i]   = arry[postleft++];

            }

        }else if (postleft >center) {

            for (int tempIndex = postright; tempIndex <= right; tempIndex++) {

                myArry[i++] = arry[tempIndex];

            }

            break;

        }else if (postright >right) {

            for (int tempIndex = postleft; tempIndex <= center; tempIndex++) {

                myArry[i++] = arry[tempIndex];

            }

            break;

        }

    }

    for (int i = left; i<= right; i++) {

        arry[i]= myArry[i];

    }

}


void mergeSort(int *arry,int left,int right)

{

     int mid = (left+right)/2;

    if (right - left > 1) {

        mergeSort(arry, left, mid);

        mergeSort(arry, mid+1, right);

    }

    merge(arry, left, mid, right);

}


void display(int * arry,int left,int right)

{

    for (int i = left; i<= right; i++) {

        printf("%d ",arry[i]);

    }

}

int main()

{

    time_t xx = time(NULL);

    for (int i = 0; i< 100000000; i++) {

        int arry[] = {7,10,19,25,12,17,21,30,48,100,-1000,30,59,43,21,34,56,87};

        mergeSort(arry, 0,  17);

    }


    printf("time use %lu s\n",time(NULL) - xx);

}


结果一(编译器优化未打开):

time use 77 s

Program ended with exit code: 0

结果二(编译器设置为最高优化):

time use 31 s

Program ended with exit code: 0


/*go语言的代码*/

package main
import (
"fmt"
"time"
)
var myArry [1000]int

func merfge(arry [] int,left int,center int ,right int){
postleft  :=left
postright :=center+1
for i:= left;i <= right;i++{
if postleft <= center && postright <= right {
if arry[postleft] > arry[postright] {
myArry[i] = arry[postright]
postright++
}else{
myArry[i] = arry[postleft]
postleft++
}
}else if postleft > center {
for tempIndex:= postright;tempIndex<= right;tempIndex++{
myArry[i] = arry[tempIndex]
i++
}
break
}else if postright > right {
for tempIndex:= postleft;tempIndex<= center;tempIndex++{
myArry[i] = arry[tempIndex]
i++
}
break
}
}
for i:= left;i<=right;i++{
arry[i]= myArry[i]
}
}

func mergesort(arry [] int ,left int,right int){
  mid :=(left + right)/2
if right - left>1{
mergesort(arry,left,mid)
mergesort(arry,mid+1,right)
}
merfge(arry,left,mid,right)
}
func  display(arry []int,num int){
for i:=0;i<=num;i++{
fmt.Println(arry[i])
}
}

func main() {
fmt.Println()
now := time.Now()
for v:=0;v<100000000;v++{
arry :=[]int{7,10,19,25,12,17,21,30,48,100,-1000,30,59,43,21,34,56,87}
mergesort(arry,0,len(arry)-1)
}
fmt.Println("时间消耗",time.Now().Second() - now.Second(),"秒")
}

结果:

时间消耗 49 秒
Process finished with exit code 0


测试结果

编程语言循环次数时间总消耗 
c1亿最快31秒 
go1亿48秒 
swift20万88秒x500 

结论:

      c语言的性能还是最强悍,swift语言的性能和苹果官方的的还是有很大差距的







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值