Calculating relative performance differences between C++ and assembly

Mystofolyse

Weaksauce
Joined
Jun 25, 2006
Messages
103
Hello, again. I'm still learning how computers work, and I've started a bit of assembly programming. AFAIK, an efficiently written assembly program will outperform a program performing the identical task in a higher language (perhaps depending on the complexity of said program?). Anyway, I'm curious about how to go about measuring the resource usage and overall processor utilization of both implementations. By the way, I understand the code of both programs below.

An assembly program (from MIPS Assembly Language Programming by Robert L. Britton (pp 20-21):
Code:
	.data	# Data declaration section
Prompt: .asciiz "\n Please Input a value for N = "
Result: .asciiz "The sum of the integers from 1 to N is "
Bye:	.asciiz "\n **** Adios Amigo - Have a good day ****"
	.globl  main
	.text

main:		# Start of code section
	li $v0, 4        # system call code for Print String
	la $a0, Prompt   # load address of prompt in $a0
	syscall		 # print the prompt message
	li $v0, 5	 # system call code for Read Integer
	syscall		 # reads the value of N into $a0
	blez $v0, End    # branch to end if $v0 <= 0
	li $t0, 0        # clear register $t0 to 0
Loop:
	add 	$t0, $t0, $v0  # sum of integers in register $t0
	addi	$v0, $v0, -1   # summing integers in reverse order
	bnez	$v0, Loop      # branch to loop if $v0 is != 0
	
	li	$v0, 4	       # system call code for Print String
	la	$a0, Result    # load address of message into $a0
	syscall		       # print the string
	
	li	$v0, 1	       # system call code for Print Integer
	move	$a0, $t0       # move value to be printed to $a0
	syscall		       # print sum of integers
	b	main	       # branch to main
	
End:
	li	$v0, 4	       # system call code for Print String
	la	$a0, Bye       # load address of msg. into $a0
	syscall		       # print the string
	li	$v0, 10	       # terminate program run and
	syscall		       # return control to system

# END OF PROGRAM
...and a C++ program accomplishing the identical task:
Code:
#include <iostream>

using namespace std;

int main(){
	int num;
	int solution;

	cout << "\n Please Input a value for N = ";
	cin >> num;

	if( num > 0 ){
		solution = 0;
		while( num > 0 )
		{
			solution = solution + num;
			num = num - 1;
		}
		cout << "   The sum of the integers from 1 to N is " << solution 
                 << endl;
	}else
		cout << "\n  **** Adios Amigo - Have a good day ****" << endl;
}

Again, any help is greatly appreciated! :)
 
I should have mentioned it before. I only have C++ Express and MIPster available for this particular question. Could you go into a little more detail?
 
Well unless you are very good at assembly todays compilers will probably convert that c++ code to better assembly then you can write yourself.
 
Assembly is only useful today for learning or for embedded processors that are very tight on ram and program size.
 
In reply to the original poster: In general, a program of any complexity at all will be far more efficient written in C/C++ than assembler. Ignoring the obvious benefits (easier and faster to write, easier to debug, more portable), humans aren't that great at seeing optimal assembler code. Most compilers will generate faster (if uglier) code than most/all humans.
 
Not to mention, if you want to measure performance, you must measure performance of something worthwhile. What is the point of optimizing the code you gave, it's trivial!? A good compiler could theoretically go all Gauss on your ass.

Also you should take care to be explicit and say that you're comparing _implementations_ of languages, not the languages themselves.
 
A profiler is a program that measures how much time your code is spending where. There are two types of profilers: sampling, and instrumented. An instrumented profiler will change your code to add timing measurements. A sampling profiler will stop your program every once in a while, figure out what it is executing, record that, and then let it continue.

Sampling profilers aren't very accurate, but don't have much influence on the code under test. Instrumented profilers are very accurate, but significantly change the program under test and require that all the code being changed is built with the instrumentation.

I'm not sure a profiler will help you much with your goal. Once you realize that one is slower than the other, a profiler might help you figure out why. With your trivial example, you should be able to figure that out by inspecting the code.

For this program, I'd just run it and time its execution. In doing that, you'll want to think about what you're timing. Are you looking for the time it takes the program to execute, or are you also worried about the time it takes the program to load from disk and initialize? The assembler code is probably smaller and doesn't spend time initializing libraries before it gets running. The C++ version is going to load at least a few libraries and do longer initialization before it gets running.

Your experiment will be fun and you'll probably learn a few interesting things. But I'm not sure the code you give here is a very good candidate. The problem is that it is obviously I/O bound. That is, most of the time that program executes is going to be spent waiting for the I/O routines to provide input. There's very little work inside the application itself. Since the code is so small the loading time might overwhem the perceived execution time, for example.

That, in itself, might teach you something about the importance of performance. When you run these programs, let's say the assembler version is 10% faster. Is it really worth finding and hiring a good assembler programmer to get a 10% win in this application? What if you bought disk drives that were 10% faster, or found the C++ I/O problem, and fixed it without leaving C++? Then, you're spending less, still getting the same win, and so on.

Can you generalize anything about that experience? Are there lots of applications which really need that much performance, or really only a few? We'd like to hope some code that we use is as fast as possible -- databases, operating systems, games, and so on -- but for most business-oriented applications, the best bet is a program that can easily be expanded to scale up on better hardware, or scale out to more hardware.

There's another thread around here where I showed how some sorting code in C++ could be written better in assembler, even assuming a very good optimizing compiler. I'll keep trying to find it, but since the search feature here sucks ass, I might come up empty.

If you'd like to pursue your project, please let me know. I'm happy to help you with it.
 
Hey, I managed to find that thread! The topic came out of a thread asking What language was windows XP built using ?. Someone there made an assertion similar to the one made here; that the average compiler makes better code than the average assembly programmer. In response, we looked at some generated code and then did some optimizations in C++ that significantly improved the generated code without actually touching the assembler.
 
Back
Top