NeomerArcana
NeomerArcana

Reputation: 2311

How do I calculate or view the number of instructions generated when compiled?

Basically, if I write a function I'd like to see that when it's compiled there are 15 instructions. Then I edit the function and see that there's now 20.

How can I measure this? Is there a tool? Do I need to learn some assembly?

Upvotes: 2

Views: 1557

Answers (2)

mip
mip

Reputation: 8733

The tool for viewing compiled code as assembly is called disassembler, but assembly output is built into most compiler suites. For gcc use gcc -S file.c command to view assembly output.

Many IDEs (e.g. Eclipse, NetBeans, Visual Studio) provide convenient windows to view such output.

You can also use online assembly viewers like http://gcc.godbolt.org/

Note that smaller number of instructions does not necessary mean that code is executing faster. Some instructions take longer time to execute than others, some may cause pipeline flush etc.

Upvotes: 3

user14717
user14717

Reputation: 5181

gcc -S source.c; wc -l source.s

Upvotes: 1

Related Questions