数据结构和算法的选择决定了-代码时间或者空间效率
2008-01-18 22:00
417 查看
/*
*不同的算法设计,
*程序运行效率是完全不一样的
author:chinayaosir
blog:http://blog.csdn.net/chinayaosir
数据结构和算法的选择决定了-代码时间或者空间效率
*功能:求500以内的素数
*本文以C代码举例
*工具:gcc,dev-c++,vc
*speed1.c times=22275
*speed2.c times=22030
*speed3.c times=907
*speed4.c times=249
*/
/*****************************************/
/*
*filename:speed1.c
*the run times=22278
*/
#include <stdio.h>
#include <stdlib.h>
int main(){
int speed_cnt=0;
int j,i;
for(i=1; i<500; i++){
for(j=2; j< i; j++) {
speed_cnt++; if(i%j == 0) break;
}
if(j >= i) printf("%d,",i);
}
printf("/nThe run times %d",speed_cnt);
return 0;
}
/*****************************************/
/*
*filename:speed2.c
*the run times=22030
*/
#include <stdio.h>
#include <stdlib.h>
int main(){
int speed_cnt=0;
int j,i;
for(i=3; i<500; i+=2){
for(j=2; j< i; j++) {
speed_cnt++; if(i%j == 0) break;
}
if(j >= i) printf("%d,",i);
}
printf("/nThe run times %d",speed_cnt);
return 0;
}
/*****************************************/
/*
*filename:speed3.c
*the run times=907
*/
#include <stdio.h>
#include <stdlib.h>
int main(){
int speed_cnt=0;
int j,i;
printf("2,");
for(i=3; i<500; i+=2){
int k = sqrt(i);
for(j=3; j<= k; j+=2){
speed_cnt++; if(i%j == 0) break;
if(j >= k ) printf("%d", i);
}
}
printf("/nThe run times %d",speed_cnt);
return 0;
}
/*****************************************/
/*
*filename:speed4.c
*the run times=249
*/
#include <stdio.h>
#include <stdlib.h>
int main(){
int speed_cnt=0;
int j,i;
printf("2,");
for(i=3; i<500; i+=2){
speed_cnt++; if(i%j == 0) break;
if(j >= i) printf("%d,", i);
}
printf("/nThe run times %d",speed_cnt);
return 0;
}
*不同的算法设计,
*程序运行效率是完全不一样的
author:chinayaosir
blog:http://blog.csdn.net/chinayaosir
数据结构和算法的选择决定了-代码时间或者空间效率
*功能:求500以内的素数
*本文以C代码举例
*工具:gcc,dev-c++,vc
*speed1.c times=22275
*speed2.c times=22030
*speed3.c times=907
*speed4.c times=249
*/
/*****************************************/
/*
*filename:speed1.c
*the run times=22278
*/
#include <stdio.h>
#include <stdlib.h>
int main(){
int speed_cnt=0;
int j,i;
for(i=1; i<500; i++){
for(j=2; j< i; j++) {
speed_cnt++; if(i%j == 0) break;
}
if(j >= i) printf("%d,",i);
}
printf("/nThe run times %d",speed_cnt);
return 0;
}
/*****************************************/
/*
*filename:speed2.c
*the run times=22030
*/
#include <stdio.h>
#include <stdlib.h>
int main(){
int speed_cnt=0;
int j,i;
for(i=3; i<500; i+=2){
for(j=2; j< i; j++) {
speed_cnt++; if(i%j == 0) break;
}
if(j >= i) printf("%d,",i);
}
printf("/nThe run times %d",speed_cnt);
return 0;
}
/*****************************************/
/*
*filename:speed3.c
*the run times=907
*/
#include <stdio.h>
#include <stdlib.h>
int main(){
int speed_cnt=0;
int j,i;
printf("2,");
for(i=3; i<500; i+=2){
int k = sqrt(i);
for(j=3; j<= k; j+=2){
speed_cnt++; if(i%j == 0) break;
if(j >= k ) printf("%d", i);
}
}
printf("/nThe run times %d",speed_cnt);
return 0;
}
/*****************************************/
/*
*filename:speed4.c
*the run times=249
*/
#include <stdio.h>
#include <stdlib.h>
int main(){
int speed_cnt=0;
int j,i;
printf("2,");
for(i=3; i<500; i+=2){
speed_cnt++; if(i%j == 0) break;
if(j >= i) printf("%d,", i);
}
printf("/nThe run times %d",speed_cnt);
return 0;
}
相关文章推荐
- 剑指offer-第五章优化时间和空间效率(在字符串中第一次出现切只出现一次的字符)
- js判断选择时间不能小于当前时间的示例代码
- js判断选择时间不能小于当前时间的示例代码
- 剑指offer-第五章优化时间和空间效率(两个链表的第一个公共节点)
- 连载:编写高效代码(8) 空间换时间——我们总是在走,却忘了停留
- 《剑指offer》刷题笔记(时间空间效率的平衡):第一个只出现一次的字符
- ExtJs根据combo的选择值决定表单项的隐藏或者显示
- 数据结构-算法效率的度量-时间复杂度和空间复杂度
- 一个在时间选择上很有用的代码
- microtime() 测试代码执行时间,提高编码效率
- extjs 时间范围选择自动判断的实现代码
- phonegap 拍照或者选择图片 上传+ 后台接收图片和参数代码
- 【代码笔记】时间选择框
- 精确记算程序的运行时间或者某段代码的运行时间
- 剑指offer——优化时间和空间效率
- 【代码笔记】提醒时间的选择
- laydate日期空间与时间选择器
- vue.js实现仿原生ios时间选择组件实例代码
- 获得毫秒级的流逝时间, 测试代码效率时很有用
- 测试代码效率时一定要用高精度的Stopwatch时间戳,避免出现误差。