Python:通过执行100万次打印来比较C和Python的性能
Python:通过执行100万次打印来比较C和python的性能,以及用C和python结合来解决性能问题的方法
python作为动态语言,开发效率相当高,但如我们所知,动态语言的执行效率往往是比较低的,请看下面简单的测试过程:
一、 C语言实现100万次打印:
代码:
- #include<stdio.h>
- #include <time.h>
- int main(int argc, char* argv[])
- {
- unsigned long i = 1;
- unsigned long ulNum = 1000000;
- clock_t start, finish;
- double duration;
- start = clock();
- while (ulNum != 0)
- {
- printf("\nThe ulNum is: %u ", i);
- ulNum--;
- i++;
- }
- finish = clock();
- duration = (double)(finish - start) / CLOCKS_PER_SEC;
- printf( "\n Use Time: %f seconds\n", duration );
- system("pause");
- return 0;
- }
测试:
可看出,执行了约489秒。
二、python实现100万次打印:
代码:
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import time
- import os
- time_begin = time.clock()
- i = 1
- ulNum = 1000000
- while (ulNum != 0):
- print "The ulNum is: %u " % i
- ulNum -= 1
- i += 1
- print "Use time: %s" % (time.clock() - time_begin)
- os.system("pause")
测试:
可看出,执行了约675秒。
相关推荐
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
淡风wisdon大大 2020-06-06
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
黎豆子 2020-05-07
xiongweiwei00 2020-04-29
Cypress 2020-04-25
冰蝶 2020-04-20