Je me suis amusé à écrire un petit test afin de comparer les performance du JAVA, du C#, et du C, et j'obtiens des résultats assez étranges, pas du tout ce que j'attendais.

Je m'attendais à avoir C > C# > JAVA, et j'ai JAVA >= C >> C# ! Oui la version JAVA est la plus rapide !

Pour ce test, j'ai fait un tri bulle d'un tableau de 25000 éléments générés aléatoirement.
Java : 1050ms
C : 1100ms (gcc -O3)
C# : 5000 1478 en compilation release

Vous en pensez quoi ? c'est mon test qui est mauvais ? Je met les 3 versions ci dessous.


Version C#
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace TestBubleSort
{
    class Program
    {
        public static long triBulle(int[] tableau)
        {
        int longueur=tableau.Length;
        bool inversion;
        long echanges=0;
        
        do
            {
            inversion=false;

            for(int i=0;i<longueur-1;i++)
                {
                if(tableau[i]>tableau[i+1])
                    {
                    echanger(tableau,i,i+1);
                    inversion=true;
                    echanges++;
                    }
                }
             }
        while(inversion);
        return echanges;
        }

        public static void echanger(int[] tableau, int i1, int i2)
        {
            int tmp = tableau[i1];
            tableau[i1] = tableau[i2];
            tableau[i2] = tmp;
        }

        static void Main(string[] args)
        {

            Stopwatch stopw = new Stopwatch();
            int[] tableau = new int[250 *100];
            Random r = new Random();
            for (int i = 0; i < tableau.Length; i++)
            {
                tableau[i] = r.Next();
            }
            //Démarrage du chrono 
            stopw.Start();
            long echanges = triBulle(tableau);
            stopw.Stop();
            
            
            //On récupère la durée écoulé, en millisecondes 
            long milliseconds = stopw.ElapsedMilliseconds;

            Console.WriteLine(echanges + "echanges, " + milliseconds + "ms");
            Console.ReadKey();
            
        }
    }
}
Version Java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class test {

	 public static long triBulle(int[] tableau)
     {
     int longueur=tableau.length;
     long echanges = 0;
     boolean inversion;
     
     do
         {
         inversion=false;

         for(int i=0;i<longueur-1;i++)
             {
             if(tableau[i]>tableau[i+1])
                 {
                 echanger(tableau,i,i+1);
                 echanges++;
                 inversion=true;
                 }
             }
          }
     while(inversion);
     
     return echanges;
     }

     public static void echanger(int[] tableau, int i1, int i2)
     {
         int tmp = tableau[i1];
         tableau[i1] = tableau[i2];
         tableau[i2] = tmp;
     }
	public static void main(String[] args) {
		int[] tableau = new int[250*100];
		for (int i = 0; i < tableau.length; i++)
        {
            tableau[i] = (int)(Integer.MAX_VALUE*Math.random());
        }
		
		long debut = System.currentTimeMillis();
		long echanges = triBulle(tableau);
		long fin = System.currentTimeMillis();
		System.out.println(echanges + "echanges, " + (fin-debut) + "ms");
		
	}

}
Version C
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define longueur 25000

void echanger(int tableau[], int i1, int i2)
{
	int tmp = tableau[i1];
	tableau[i1] = tableau[i2];
	tableau[i2] = tmp;
}
long triBulle(int tableau[])
{
	
	int inversion;
	long echanges=0;

	do
	{
		inversion=0;
		int i;
		for(i=0;i<longueur-1;i++)
		{
			if(tableau[i]>tableau[i+1])
			{
				echanger(tableau,i,i+1);
				inversion=1;
				echanges++;
			}
		}
	}
	while(inversion);
	
	return echanges;
}



int main(void)
{
	int *tableau = malloc(longueur*4);
	srand(time(NULL));
	int i;
	for (i = 0; i < longueur; i++)
	{
		tableau[i] = rand();
	}
	clock_t start, finish;
	
	start = clock();
	long echanges = triBulle(tableau);
	finish = clock();
	double seconds = (double)(finish - start) / CLOCKS_PER_SEC; 
	
	printf("%ld echanges, %f secondes\n",echanges, seconds);
}