我在c中有这么简单的代码:
#include <stdio.h>
void test() {}
int main()
{
if (2 < 3) {
int zz = 10;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我看到这段代码的汇编输出时:
test():
pushq %rbp
movq %rsp, %rbp
nop
popq %rbp
ret
main:
pushq %rbp
movq %rsp, %rbp
movl $10, -4(%rbp) // space is created for zz on stack
movl $0, %eax
popq %rbp
ret
Run Code Online (Sandbox Code Playgroud)
我从这里 得到了程序集(默认选项) 我看不出条件检查的指令在哪里?
我做了一个简单的性能比较,侧重于使用C#的浮点运算,针对带有Windows 10 IoT的Raspberry Pi 3 Model 2,我将它与Intel Core i7-6500U CPU @ 2.50GHz进行了比较.
Raspberry Pi 3 Model B V1.2 - 测试结果 - 图表
英特尔酷睿i7-6500U CPU @ 2.50GHz - x64测试结果 - 图表
英特尔酷睿i7 仅比Raspberry Pi 3 快十二倍(x64)! - 根据那些测试.
准确度为11.67,并计算每个平台在这些测试中实现的最佳性能.两个平台在并行运行的四个线程中实现了最佳性能(非常简单,独立的计算).
问题:测量和比较这些平台的计算性能的正确方法是什么?目的是比较优化算法,机器学习算法,统计分析等领域的计算性能.因此,我的重点是浮点运算.
有一些基准测试(如MWIPS)和MIPS或FLOPS等测量.但我没有找到一种方法来比较不同的CPU平台的计算能力.
我找到了Roy Longbottom的一个比较(谷歌"Roy Longbottom的Raspberry Pi,Pi 2和Pi 3基准" - 我不能在这里发布更多链接)但根据他的基准测试,Raspberry Pi 3的速度只比英特尔酷睿i7快4倍(x64)建筑,MFLOPS比较).与我的结果非常不同.
以下是我执行的测试的详细信息:
测试是围绕应该迭代执行的简单操作构建的:
private static float SingleAverageCalc(float seed, long nTimes)
{
float x1 = seed, x2 = …Run Code Online (Sandbox Code Playgroud) 我有一个优化for循环的任务,所以编译器编译运行得更快的代码.目标是使代码在5秒或更短的时间内运行,原始运行时间约为23秒.原始代码如下所示:
#include <stdio.h>
#include <stdlib.h>
#define N_TIMES 600000
#define ARRAY_SIZE 10000
int main(void)
{
double *array = calloc(ARRAY_SIZE, sizeof(double));
double sum = 0;
int i;
printf("CS201 - Asgmt 4 - I. Forgot\n");
for (i = 0; i < N_TIMES; i++) {
int j;
for (j = 0; j < ARRAY_SIZE; j++) {
sum += array[j];
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的第一个想法是在内部for循环上进行循环展开,使其下降到5.7秒,并且该循环看起来像这样:
for (j = 0; j < ARRAY_SIZE - 11; j+= 12) {
sum = sum + (array[j] + array[j+1] …Run Code Online (Sandbox Code Playgroud) 编辑 -我的构建系统有问题。我仍在弄清楚到底是什么,但是gcc产生了奇怪的结果(即使它是一个.cpp文件),但是一旦使用,g++它就会按预期工作。
对于我一直遇到的问题,这是一个非常减少的测试用例,其中使用数字包装器类(我认为应该内联)使我的程序慢10倍。
这与优化级别无关(使用-O0和尝试-O3)。
我在包装器类中缺少一些细节吗?
我有以下程序,其中定义了一个包装a double并提供+操作符的类:
#include <cstdio>
#include <cstdlib>
#define INLINE __attribute__((always_inline)) inline
struct alignas(8) WrappedDouble {
double value;
INLINE friend const WrappedDouble operator+(const WrappedDouble& left, const WrappedDouble& right) {
return {left.value + right.value};
};
};
#define doubleType WrappedDouble // either "double" or "WrappedDouble"
int main() {
int N = 100000000;
doubleType* arr = (doubleType*)malloc(sizeof(doubleType)*N);
for (int i = 1; i < N; …Run Code Online (Sandbox Code Playgroud) 我有一个任务,我必须采取一个程序,并使其在时间上更有效.原始代码是:
#include <stdio.h>
#include <stdlib.h>
// You are only allowed to make changes to this code as specified by the comments in it.
// The code you submit must have these two values.
#define N_TIMES 600000
#define ARRAY_SIZE 10000
int main(void)
{
double *array = calloc(ARRAY_SIZE, sizeof(double));
double sum = 0;
int i;
// You can add variables between this comment ...
long int help;
// ... and this one.
// Please change 'your name' to your actual name.
printf("CS201 …Run Code Online (Sandbox Code Playgroud) c ×3
assembly ×2
optimization ×2
performance ×2
c# ×1
c++ ×1
c++11 ×1
compilation ×1
for-loop ×1
intel ×1
raspberry-pi ×1