汇编能使用 C 编写的程序更快吗? [英] Can assembly make program written in C faster?

查看:47
本文介绍了汇编能使用 C 编写的程序更快吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C 语言编写程序,需要让它更快,因为这是对性能的评估.所以我很好奇汇编代码是否可以使 C 程序在任何级别上都更快?如果我用汇编替换部分 C 代码,是否可以缩短程序的运行时间?(例如巨大的 for 循环).\

I am writing program in C language and need to make it significantly faster as this is assessment for performance. So I am curious if Assembly code can make C program faster at any level? Is it possible to make runtime of program shorter if I replace some fractions of C code by assembly? (for example huge for loops).\

谢谢.

推荐答案

用汇编替换 C 代码可以使您的代码更快当且仅当以下一项或多项为真:

Replacing C code with assembly can make your code faster if and only if one or more of the following is true:

  1. 你的编译器正在生成糟糕的代码.

  1. Your compiler is generating terrible code.

您忘记启用优化.

您要替换的 C 代码异常低效.

The C code you are replacing was unusually inefficient.

您正在编写的程序集利用了编译器无法使用的 CPU 功能,例如向量运算或特定于任务的原语,例如加密加速.(请注意,一些现代编译器也可以自动对代码进行矢量化处理,尽管并不总是很好.)

The assembly you are writing is making use of CPU features that the compiler cannot, such as vector operations or task-specific primitives like crypto acceleration. (Note that some modern compilers can automatically vectorize code, too, albeit not always very well.)

如果以上条件都不成立,那你就是在浪费时间.

If none of the preceding conditions is true, you'll be wasting your time.

这篇关于汇编能使用 C 编写的程序更快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆