# swift 性能弱爆了

 编程   语言 循环次数 时间总  消耗 c 1亿 最快31秒 go 1亿 48秒 swift 20万 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)")

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(),"秒")
}

Process finished with exit code 0

 编程语言 循环次数 时间总消耗 c 1亿 最快31秒 go 1亿 48秒 swift 20万 88秒x500

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

• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 2
评论
08-03 1万+
04-17 989
07-21 621
08-19
12-04
01-16 1万+
10-26 4395
06-23 5345
07-23 2万+
05-27 492
07-31 5万+
11-02 7685
04-28 182

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

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