python运行效率低下_有没有实际的例子来说明python的效率低下

一个使用插入排序的实际比较,如您所见,C要快得多。请注意,这些都是1对1的尝试,在现实世界中,您只需使用Python的sort,它使用https://en.wikipedia.org/wiki/Timsort,效率要高得多。结果:

Pythonreal 0m0.034s

user 0m0.028s

sys 0m0.008s

C

^{pr2}$

Python中的第一个#!/usr/bin/python

a = [16, 7, 4, 10, 18, 15, 6, 12, 13, 5, 11, 14, 17, 8, 2, 9, 19, 3, 1]

print 'Unsorted: %s' % a

def insertion_sort(a):

for j in range(1, len(a)):

key = a[j]

i = j - 1

while i >= 0 and a[i] > key:

a[i+1] = a[i]

i = i - 1

a[i+1] = key

return a

# execute the sort

print 'Sorted: %s' % insertion_sort(a)

C中的第二个#include

#include

/*

Compile with:

cc insertion-sort.c -o insertion-sort

*/

int main(int argc, char **argv)

{

int a[20] = {16, 7, 4, 10, 18, 15, 6, 12, 13, 5, 11, 14, 17, 8, 2, 9, 20, 19, 3, 1};

int i, j, key;

int len = 20;

printf("Unsorted: [");

for ( i = 0; i < len; i++ ) {

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

}

printf("]\n");

for ( j = 0 ; j < len ; j++ )

{

key = a[j];

i = j - 1;

while ( i >= 0 && a[i] > key ) {

a[i + 1] = a[i];

i = i - 1;

}

a[i + 1] = key;

}

printf("Sorted: [");

for ( i = 0; i < len; i++ ) {

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

}

printf("]\n");

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值