Question: One of my senior collegues optimizes a function (he is implementing image filtering) by writing in-line assembly. Is that really necessary? Wouldn't modern compiler do that for us? Typically, how much gain do we have by converting C code into assembly? If assembly code really brings lots of benefits, when should we convert C/C++ code into assembly and when should we leave the code as it is, since assembly code is hard to read and maintain.

Answer: 
If you are smarter than the compiler, you *may* be able to make your code faster on one specific platform by writing it by hand in assembly.

However, most big C/C++ compilers are *extremely* good optimizers; you are unlikely to be smarter than them.
> 
> One of my senior collegues optimizes a function (he is implementing image filtering) by writing in-line assembly. Is that really necessary?
> 
> 
> 

Obviously I can't comment on your colleagues exact situation, but I wouldn't be surprised if it was necessary. There's many specialised instructions that are used for image filters that won't necessarily be used by the compiler. Inline assembly is often the only way to access those instructions (or through intrinsics).

> 
> Wouldn't modern compiler do that for us?
> 
> 
> 

Obviously this depends on what *'that'* is, but while modern compilers are certainly good at generating code, **they aren't magic**. It is often the case where you know something about your code that the compiler doesn't (or can't).

If your line of work involves high performance code then there are definitely places where you can get major improvements from using inline assembly (or even just compiler intrinsics).

> 
> If assembly code really brings lots of benefits, when should we convert C/C++ code into assembly and when should we leave the code as it is, since assembly code is hard to read and maintain.
> 
> 
> 

Here's how:

1. First, profile your code to see what potential benefits are to be gained.
2. Look at the disassembly to see what the compiler is doing. If it is already doing things optimally then there is no point going further.
3. If there are opportunities for improvement, consider using compiler intrinsics before hand-written assembly as it is generally easier to maintain and more portable.
4. Only if all that fails should you go to inline assembly.