Inside LabVIEW - How the Compiler Works

Sunday, December 21, 2003

A very nice arcitle published now on NI web site...

Long time ago I had asked NI engineers "where is a code?". After true "C" compiler I can take a look to asm code, and then looking for bottlenecks, code efficiency and so on... Now we can obtain asm listing for LabVIEW code.

OK, following example will be used for comparison:

C code:

#define ARRAY_SIZE 50000000
for(i = 0; i < ARRAY_SIZE; i++){
    if(array[i] == 0) count++;
}

Source above produced following code:
      xor edx, edx                                
.B1.5:                          
      mov       ecx, DWORD PTR [esi+edx*4]                
      add       edx, 1                                    
      test      ecx, ecx                                 
      jne       .B1.7                          
      add       edi, 1                                
.B1.7:                         
      cmp       edx, 50000000                                
      jl        .B1.5                                         

(Intel C++ compiler was used.)

Now LabVIEW code:

LabVIEW code produced following output (you can found full listing on NI web site - see links):

	  mov    eax, dword ptr [esi]       ;adjust pointers...	
 	  sub    eax, dword ptr [esi+8]     ;...check for empty array...	
 	  mov    dword ptr [esi], eax	 	
 	  lea    edi, [ebp+2D4h]	 	
 	  mov    dword ptr [edi], eax	 	
 	  cmp    dword ptr [ebp+2DCh], 0	 	
 	  jg     270	 	
 	  jmp    2DD
	270:                                ;MAIN LOOP
	  lea    esi, [ebp+2ECh]	 	
 	  mov    eax, dword ptr [esi]       ;advance pointers...	
 	  add    eax, dword ptr [esi+8]	 	
 	  mov    dword ptr [esi], eax	 	
 	  lea    edi, [ebp+2D4h]	 	
 	  mov    dword ptr [edi], eax	 	
 	  mov    eax, dword ptr [ebp+2D4h]	 	
 	  mov    eax, dword ptr [eax]       ;check if element is zero...
 	  cmp    eax, 0	 	
 	  je     29D	 	
 	  jmp    2AE	 	
 	29D:  
 	  mov    eax, dword ptr [ebp+2D0h]	 	
 	  add    eax, 1                     ;add one to value...
 	  mov    dword ptr [ebp+2D0h], eax	 	
 	2AE:  
 	  mov    eax, dword ptr [ebp+68h]   ;check timer	
 	  cmp    dword ptr [eax+18h], 0     ;process UI events	
 	  je     3AC                        ;(for Abort button, moving...)	
 	  mov    eax, dword ptr [ebp+2DCh]	 	
 	  mov    ecx, dword ptr [ebp+2D8h]	 	
 	  add    ecx, 1                     ;increment loop counter	
 	  cmp    ecx, eax                   ;and test for last iteration	
 	  jge    2DD	 	
 	  mov    dword ptr [ebp+2D8h], ecx	 	
 	  jmp    270                        ;*** END of LOOP
 	2DD:
Now we can see, that LabVIEW generated code not very effective, and have a lot "overheads". As result, LabVIEW code up to 4 times more slowly. (C code - 280 milliseconds, but LabVIEW - 1100 ms)