cpu数组不同的访问方式的性能测试
2014-04-30 13:29
274 查看
性能测试 程序输出:
1:temp = array[i]*i: 2430.0 ms
2:temp = GET(array,i) *i: 2430.0 ms
3:temp = get(array,i)*i: 2840.0 ms
4:int a = get(array,i);temp = a*i: 3370.0 ms
5:int a = array[i];temp = a*i;: 2010.0 ms
6:temp = *(array+i)*i: 2430.0 ms
7:temp = *(array+i)*i: 2010.0 ms
其中,GET是宏定义,get是函数。
显然,1,2,6是一样的,符合预期。(当然有时不一样,但很接近)
3 通过函数调用来访问,时间长点,符合预期。
4 在3的基础上,还多了次赋值,时间长电,符合预期。
5和7 就不可思议了,她在1的基础上错了次赋值,时间竟然变短了。
测试代码
发现貌似是乘以i的原因导致那个奇怪的问题,
把乘以i改为乘以1或者2之类的,貌似就不会的。
但是如果是乘以i+1的话还是很不符合预期。
看来要看一下汇编代码。
1:temp = array[i]*i: 2430.0 ms
2:temp = GET(array,i) *i: 2430.0 ms
3:temp = get(array,i)*i: 2840.0 ms
4:int a = get(array,i);temp = a*i: 3370.0 ms
5:int a = array[i];temp = a*i;: 2010.0 ms
6:temp = *(array+i)*i: 2430.0 ms
7:temp = *(array+i)*i: 2010.0 ms
其中,GET是宏定义,get是函数。
显然,1,2,6是一样的,符合预期。(当然有时不一样,但很接近)
3 通过函数调用来访问,时间长点,符合预期。
4 在3的基础上,还多了次赋值,时间长电,符合预期。
5和7 就不可思议了,她在1的基础上错了次赋值,时间竟然变短了。
测试代码
#include <stdlib.h> #include <stdio.h> #include <iostream> using namespace std; #define GET(array,index) array[index] int get(int *array,int index) { return array[index]; } int main(void) { clock_t start,stop; float elapsedTime; int rounds = 10000; int array_size = 100000; int *array = new int[array_size]; for(int i=0;i<array_size;i++) { array[i]=i; } int temp = 0; start = clock(); for(int round=0;round<rounds;round++) { for(int i=0;i<array_size;i++) { temp = array[i]*i; } } stop= clock(); elapsedTime = (float)(stop - start) / (float)CLOCKS_PER_SEC * 1000.0f; printf( "1:temp = array[i]*i: %3.1f ms\n", elapsedTime ); start = clock(); for(int round=0;round<rounds;round++) { for(int i=0;i<array_size;i++) { temp = GET(array,i) *i; } } stop= clock(); elapsedTime = (float)(stop - start) / (float)CLOCKS_PER_SEC * 1000.0f; printf( "2:temp = GET(array,i) *i: %3.1f ms\n", elapsedTime ); start = clock(); for(int round=0;round<rounds;round++) { for(int i=0;i<array_size;i++) { temp = get(array,i)*i; } } stop= clock(); elapsedTime = (float)(stop - start) / (float)CLOCKS_PER_SEC * 1000.0f; printf( "3:temp = get(array,i)*i: %3.1f ms\n", elapsedTime ); start = clock(); for(int round=0;round<rounds;round++) { for(int i=0;i<array_size;i++) { int a = get(array,i); temp = a*i; } } stop= clock(); elapsedTime = (float)(stop - start) / (float)CLOCKS_PER_SEC * 1000.0f; printf( "4:int a = get(array,i);temp = a*i: %3.1f ms\n", elapsedTime ); start = clock(); for(int round=0;round<rounds;round++) { for(int i=0;i<array_size;i++) { int a = array[i]; temp = a*i; } } stop= clock(); elapsedTime = (float)(stop - start) / (float)CLOCKS_PER_SEC * 1000.0f; printf( "5:int a = array[i];temp = a*i;: %3.1f ms\n", elapsedTime ); start = clock(); for(int round=0;round<rounds;round++) { for(int i=0;i<array_size;i++) { temp = *(array+i)*i; } } stop= clock(); elapsedTime = (float)(stop - start) / (float)CLOCKS_PER_SEC * 1000.0f; printf( "6:temp = *(array+i)*i: %3.1f ms\n", elapsedTime ); start = clock(); for(int round=0;round<rounds;round++) { for(int i=0;i<array_size;i++) { int a = *(array+i); temp = a*i; } } stop= clock(); elapsedTime = (float)(stop - start) / (float)CLOCKS_PER_SEC * 1000.0f; printf( "7:temp = *(array+i)*i: %3.1f ms\n", elapsedTime ); return 0; }
发现貌似是乘以i的原因导致那个奇怪的问题,
把乘以i改为乘以1或者2之类的,貌似就不会的。
但是如果是乘以i+1的话还是很不符合预期。
看来要看一下汇编代码。
相关文章推荐
- NME中不同渲染方式的性能测试
- 使用4种不同的方式访问数组中的元素
- 指针和数组访问方式的不同
- 使用数组方式访问Oracle,提高性能
- 以指针方式访问数组几种不同形式输出语句解析
- 数据不同方式的提取和提交的性能测试(转载)
- 打印不同的数(编写一个程序,使用指针访问数组方式,读入十个数,输出其中不同的数,即一个数如果出现多次,只打印一次)
- 不同存储类型在Ubuntu下的性能测试
- sql语句查询条件的不同表达方式对查询性能的影响
- FEP: 测试 docker matrix, 获得 CPU, MEMORY, 网络性能的 统计信息
- JS常用的几种数组遍历方式以及性能分析对比实例详解
- web-xml与server-xml配置不同形成应用不同的访问方式
- 使用SQL Server 2005 新的语法ROW_NUMBER()进行分页的两种不同方式的性能比较
- 单数据库,多数据库,单实例,多实例不同情况下的数据访问效率测试
- jQuery各种show/hide方式的性能测试 .
- Leetcode667. 求一种数组排列方式使数组相邻数值对有k个不同绝对值
- JS几种数组遍历方式以及性能分析对比
- Java Map遍历方式方式及性能测试
- 性能测试指标:CPU Load和CPU利用率的关系
- 测试docker不同主机间容器互相访问