Click here to Skip to main content
11,934,783 members (48,701 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked

Inner Product Experiment: C# vs. C/C++

, 20 May 2008 GPL3
Rate this:
Please Sign up or sign in to vote.
The article demonstrating speed of inner product operation performed with shorts, ints, longs, floats, doubles and decimals in C# compared to C/C++


The inner product (or dot product, scalar product) operation is the major one in digital signal processing field. It is used everywhere, Fourier (FFT, DCT), wavelet-analysis, filtering operations and so on. After written the similar article for the inner product in C/C++ Inner Product Experiment: CPU, FPU vs. SSE* I was thinking how the same code written in C# will perform. I repeated the inner product operations using C# types: shorts, ints, longs, floats, doubles and decimals.


Inner Product Experiment: CPU, FPU vs. SSE*

Using the code

Just run the inner.exe providing as an argument the size of vector you want to convolve with. Make sure you placed timer.dll in the same directory with the executable. It provides tic() and toc() functions implementing precision time counter in milliseconds. I use the dll in PerformanceCounter static class in functions PerformanceCounter.Tic() and PerformanceCounter.Toc().

static public class PerformanceCounter

//Enums, Structs, Classes


        static extern void tic();
        static extern long toc(); 

        static public long Tic()
                        return 0;
                catch (Exception e)
                        Console.WriteLine(String.Format("PerformanceCounter.Tic() {0}", e.Message));
                        return -1;

        static public long Toc()
                        return toc();
                catch (Exception e)
                        Console.WriteLine(String.Format("PerformanceCounter.Toc() {0}", e.Message));
                        return -1;



The main console body contains that code. I included only doubles function here to save space:

class Program
        static int size = 1000000;

        static void Main(string[] args)
                        if (args.Length >= 1)
                                size = (int)Convert.ToUInt32(args[0]);
                catch (Exception e)
                        Console.WriteLine(String.Format("Can not convert {0} to uint32: {1}", args[0], e.Message));
                        size = 1000000;


        static void doubles()
                double[] a = new double[size];
                double[] b = new double[size];

                Random rnd = new Random();
                for (int i = 0; i < size; i++)
                        a[i] = rnd.NextDouble() - 0.5;
                        b[i] = rnd.NextDouble() - 0.5;


                double c = 0.0;
                for (int i = 0; i < size; i++)
                        c += a[i] * b[i];

                Console.WriteLine(String.Format(" doubles: {0} ms", PerformanceCounter.Toc()));

                a = null;
                b = null;


Below is the example of the console output for 5000000 dimensional vectors.

>inner.exe 5000000
 shorts: 16 ms
 ints: 7 ms
 longs: 69 ms
 floats: 9 ms
 doubles: 9 ms
 decimals: 2569 ms

I was actually stunned seeing floats and doubles in C# performing 1.3 to 3.3 times faster than in C/C++ even SSE optimized. It should not be so, as the code is managed and compiled during run-time and it is the same CPU/FPU? but how is it possible to run faster? If you now the answer post it here. See the Inner Product Experiment: CPU, FPU vs. SSE* article on the performance times for corresponding numeric types in C/C++. Ints perform a little faster but it might be of no profit quantizing floats to fixed point arithmetic and C# again outperforms C/C++ runing 2.28 times faster. However shorts and longs run quite slow. Shorts in C# perform as fast as in C/C++ but SSE2 intrinsics however outperform C#. You should prevent yourself to not to use decimals until you need high precision after comma, otherwise it will run the computation forever.

Having all that amenities in C# programming shall we not migrate DSP applications from C++?

Update (7 Apr 2008)

Sadly to C# adherents and to great delight of C++ gurus as the labours we spent in C/C++ were not yet in vain. The C# compiler indeed optimizes the code the way to avoid unused variables somehow, that indeed led me astray. To regain tarnished C++ glory here is the example of C# output for 5000000 sized vectors:

>inner.exe 5000000
 shorts: 16 ms 
 ints: 18 ms 
 longs: 72 ms 
 floats: 30 ms 
 doubles: 35 ms 
 decimals: 2936 ms 

It leaves however some space for dispute as why it does not removed unused for() for shorts and longs. The doubles run slower compared to floats contrariwise for C++ where doubles outperforms floats.

Update (6 May 2008)

Unfolding for() loops indeed provided speed up but only in case of unfolding 4 times. The same trick did not provided performance increase in C++ code. This is how I did the unfolding:

float c = 0.0f;
int ii = 0;
for (int i = 0; i < size / 4; i++)
        c += a[ii] * b[ii];
        c += a[ii] * b[ii];
        c += a[ii] * b[ii];
        c += a[ii] * b[ii];

And the results are shown below:

>inner.exe 5000000
 shorts: 16 ms
 shorts 4loop: 14 ms
 ints: 18 ms
 ints 4loop: 16 ms
 longs: 71 ms
 longs 4loop: 75 ms
 floats: 32 ms
 floats 4loop: 15 ms
 doubles: 34 ms
 doubles 4loop: 24 ms
 doubles unsafe: 32 ms
 decimals: 2550 ms
 decimals 4loop: 2611 ms


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Chesnokov Yuriy
Russian Federation Russian Federation
Highly skilled Engineer with 14 years of experience in academia, R&D and commercial product development supporting full software life-cycle from idea to implementation and further support. During my academic career I was able to succeed in MIT Computers in Cardiology 2006 international challenge, as a R&D and SW engineer gain CodeProject MVP, find algorithmic solutions to quickly resolve tough customer problems to pass product requirements in tight deadlines. My key areas of expertise involve Object-Oriented
Analysis and Design OOAD, OOP, machine learning, natural language processing, face recognition, computer vision and image processing, wavelet analysis, digital signal processing in cardiology.

You may also be interested in...

Comments and Discussions

QuestionWhy the suprise? Pin
adamvanner15-Feb-09 10:10
memberadamvanner15-Feb-09 10:10 
GeneralC/C++ performance Pin
nickythequick27-May-08 5:19
membernickythequick27-May-08 5:19 
Generalnice results but... Pin
dmihailescu20-May-08 7:39
memberdmihailescu20-May-08 7:39 
GeneralRe: nice results but... Pin
Chesnokov Yuriy26-May-08 22:20
mvpChesnokov Yuriy26-May-08 22:20 
NewsStephen Hewitt and reinux code results. Look here anyone please before blaiming my article Pin
Chesnokov Yuriy8-Apr-08 22:15
mvpChesnokov Yuriy8-Apr-08 22:15 
GeneralRe: Stephen Hewitt and reinux code results. Look here anyone please before blaiming my article Pin
dshorter19-Apr-08 12:20
memberdshorter19-Apr-08 12:20 
GeneralRe: Stephen Hewitt and reinux code results. Look here anyone please before blaiming my article Pin
Stephen Hewitt9-Apr-08 20:10
mvpStephen Hewitt9-Apr-08 20:10 
AnswerRe: Stephen Hewitt and reinux code results. Look here anyone please before blaiming my article Pin
Chesnokov Yuriy26-May-08 22:25
mvpChesnokov Yuriy26-May-08 22:25 
GeneralRe: Stephen Hewitt and reinux code results. Look here anyone please before blaiming my article Pin
reinux10-Apr-08 22:39
memberreinux10-Apr-08 22:39 
GeneralRe: Stephen Hewitt and reinux code results. Look here anyone please before blaiming my article Pin
Marcin Smialek2-Oct-08 22:51
memberMarcin Smialek2-Oct-08 22:51 
Generalscalar product and senseless results Pin
Sigismondo Boschi8-Apr-08 5:55
memberSigismondo Boschi8-Apr-08 5:55 
AnswerRe: scalar product and senseless results Pin
Chesnokov Yuriy8-Apr-08 21:20
mvpChesnokov Yuriy8-Apr-08 21:20 
GeneralRe: scalar product and aplogizes Pin
Sigismondo Boschi9-Apr-08 22:58
memberSigismondo Boschi9-Apr-08 22:58 
GeneralUnroll those loops Pin
Jean-Paul Mikkers7-Apr-08 10:01
memberJean-Paul Mikkers7-Apr-08 10:01 
GeneralRe: Unroll those loops Pin
Stephen Hewitt7-Apr-08 16:38
mvpStephen Hewitt7-Apr-08 16:38 
GeneralRe: Unroll those loops Pin
Jean-Paul Mikkers8-Apr-08 8:09
memberJean-Paul Mikkers8-Apr-08 8:09 
GeneralRe: Unroll those loops 4-6 times faster Pin
bcarpent122820-May-08 10:22
memberbcarpent122820-May-08 10:22 
GeneralOOPS Unroll those loops 4-6 times correction Pin
bcarpent122820-May-08 10:41
memberbcarpent122820-May-08 10:41 
GeneralUnsafe C# [modified] Pin
reinux5-Apr-08 12:17
memberreinux5-Apr-08 12:17 
GeneralYou are not using result of calculation... Pin
mihasik3-Apr-08 9:35
membermihasik3-Apr-08 9:35 
GeneralRe: You are not using result of calculation... Pin
reinux5-Apr-08 11:53
memberreinux5-Apr-08 11:53 
GeneralRe: You are not using result of calculation... Pin
Stephen Hewitt6-Apr-08 20:02
mvpStephen Hewitt6-Apr-08 20:02 
GeneralMy programs begs to differ! Pin
Stephen Hewitt2-Apr-08 12:29
mvpStephen Hewitt2-Apr-08 12:29 
GeneralRe: My programs begs to differ! Pin
Chesnokov Yuriy3-Apr-08 0:18
mvpChesnokov Yuriy3-Apr-08 0:18 
GeneralRe: My programs begs to differ! Pin
Stephen Hewitt6-Apr-08 16:31
mvpStephen Hewitt6-Apr-08 16:31 
GeneralRe: My programs begs to differ! Pin
reinux5-Apr-08 11:45
memberreinux5-Apr-08 11:45 
GeneralExcellent Pin
merlin9812-Apr-08 6:53
membermerlin9812-Apr-08 6:53 
GeneralRe: Excellent Pin
Stephen Hewitt2-Apr-08 12:32
mvpStephen Hewitt2-Apr-08 12:32 
GeneralVista x64 Problems Pin
SubMash2-Apr-08 0:21
memberSubMash2-Apr-08 0:21 
AnswerRe: Vista x64 Problems Pin
Chesnokov Yuriy2-Apr-08 1:10
mvpChesnokov Yuriy2-Apr-08 1:10 
GeneralMy results... Pin
Stephen Hewitt1-Apr-08 20:56
mvpStephen Hewitt1-Apr-08 20:56 
AnswerRe: My results... Pin
Chesnokov Yuriy1-Apr-08 23:21
mvpChesnokov Yuriy1-Apr-08 23:21 
GeneralNice Article Pin
Eric Haddan1-Apr-08 9:33
memberEric Haddan1-Apr-08 9:33 
GeneralRe: Nice Article [modified] Pin
Nenad L.1-Apr-08 22:47
memberNenad L.1-Apr-08 22:47 
AnswerRe: Nice Article Pin
Chesnokov Yuriy1-Apr-08 23:13
mvpChesnokov Yuriy1-Apr-08 23:13 
GeneralRe: Nice Article Pin
Christoph Weber2-Apr-08 0:02
memberChristoph Weber2-Apr-08 0:02 
GeneralRe: Nice Article Pin
Eric Haddan2-Apr-08 3:57
memberEric Haddan2-Apr-08 3:57 
GeneralRe: Nice Article Pin
Member 11621128-Apr-08 4:13
memberMember 11621128-Apr-08 4:13 
GeneralMy results.. Pin
Livid1-Apr-08 6:33
memberLivid1-Apr-08 6:33 
AnswerRe: My results.. Pin
Chesnokov Yuriy1-Apr-08 8:24
mvpChesnokov Yuriy1-Apr-08 8:24 
GeneralRe: My results.. Pin
Livid1-Apr-08 8:54
memberLivid1-Apr-08 8:54 
AnswerRe: My results.. Pin
Chesnokov Yuriy1-Apr-08 20:50
mvpChesnokov Yuriy1-Apr-08 20:50 
GeneralRe: My results.. Pin
Gunnar8-Apr-08 4:14
memberGunnar8-Apr-08 4:14 
Generalnot significant, since... Pin
toxcct1-Apr-08 3:26
mvptoxcct1-Apr-08 3:26 
AnswerRe: not significant, since... you're wrong Pin
Chesnokov Yuriy1-Apr-08 5:09
mvpChesnokov Yuriy1-Apr-08 5:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 20 May 2008
Article Copyright 2008 by Chesnokov Yuriy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid