IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Java Discussion :

Optimisation Vs JVM


Sujet :

Java

  1. #21
    Membre confirmé
    Inscrit en
    Juillet 2009
    Messages
    93
    Détails du profil
    Informations forums :
    Inscription : Juillet 2009
    Messages : 93
    Par défaut
    merci de ces conseils
    Perso, j'avais toujours considérer les fonctions natives comme bien plus optimisée et du coup suite à vos reflexion, j'ai voulu faire un test
    comparaison du natif et du recodé
    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
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    public class Test2 {
        private static int tokenNumber = 12;
        private static int tokenLength = 12;
     
        public static void main(String[] args) {
            // declaration
            StringBuilder chaineB = new StringBuilder();
            Long[] resultIndexOfNatif = new Long[tokenNumber + 1];
            Long[] resultIndexOf = new Long[tokenNumber + 1];
            String[] str = new String[tokenNumber];
            // generate chaine and str[]
            for (int j = 0; j < tokenNumber; j++) {
                str[j] = generateString(tokenLength);
                chaineB.append(str[j]);
                chaineB.append(" ");
            }
            System.out.println(chaineB);
            String chaine = chaineB.toString();
            // Chronometer IndexOfNatif
            long top = System.currentTimeMillis();
            for (int j = 0; j < tokenNumber; j++) {
                long tmp = System.currentTimeMillis();
                callIndexOfNatif(chaine, str[j]);
                resultIndexOfNatif[j] = System.currentTimeMillis() - tmp;
            }
            resultIndexOfNatif[tokenNumber] = System.currentTimeMillis() - top;
            // print result
            System.out.println("Resultat IndexOfNatif : ");
            for (int j = 0; j < tokenNumber + 1; j++) {
                System.out.println(j + " : " + resultIndexOfNatif[j]);
            }
            // Chronometer IndexOf recodé
            long top2 = System.currentTimeMillis();
            for (int j = 0; j < tokenNumber; j++) {
                long tmp = System.currentTimeMillis();
                callIndexOf(chaine, str[j]);
                resultIndexOf[j] = System.currentTimeMillis() - tmp;
            }
            resultIndexOf[tokenNumber] = System.currentTimeMillis() - top2;
            // print result
            System.out.println("Resultat IndexOf recodé : ");
            for (int j = 0; j < tokenNumber + 1; j++) {
                System.out.println(j + " : " + resultIndexOf[j]);
            }
        }
     
        private static void callIndexOf(String chaine, String string) {
            for (int i = 0; i < 10000000; i++) {
                indexOf(chaine, string);
            }
        }
     
        private static void callIndexOfNatif(String chaine, String string) {
            for (int i = 0; i < 10000000; i++) {
                chaine.indexOf(string);
            }
        }
     
        public static int indexOf(String source, String str) {
            return indexOf(source, str, 0);
        }
     
        public static int indexOf(String source, String str, int fromIndex) {
            return indexOf(source.toCharArray(), 0, source.length(), str
                    .toCharArray(), 0, str.length(), fromIndex);
        }
     
        static int indexOf(char[] source, int sourceOffset, int sourceCount,
                char[] target, int targetOffset, int targetCount, int fromIndex) {
            if (fromIndex >= sourceCount) {
                return (targetCount == 0 ? sourceCount : -1);
            }
            if (fromIndex < 0) {
                fromIndex = 0;
            }
            if (targetCount == 0) {
                return fromIndex;
            }
     
            char first = target[targetOffset];
            int max = sourceOffset + (sourceCount - targetCount);
     
            for (int i = sourceOffset + fromIndex; i <= max; i++) {
                /* Look for first character. */
                if (source[i] != first) {
                    while (++i <= max && source[i] != first)
                        ;
                }
     
                /* Found first character, now look at the rest of v2 */
                if (i <= max) {
                    int j = i + 1;
                    int end = j + targetCount - 1;
                    for (int k = targetOffset + 1; j < end
                            && source[j] == target[k]; j++, k++)
                        ;
     
                    if (j == end) {
                        /* Found whole string. */
                        return i - sourceOffset;
                    }
                }
            }
            return -1;
        }
     
        private static String chars = "abcdefghijklmnopqrstuvwxyz";
        private static int charLength = chars.length();
     
        public static String generateString(int length) {
            StringBuilder pass = new StringBuilder(charLength);
            for (int x = 0; x < length; x++) {
                int i = (int) (Math.random() * charLength);
                pass.append(chars.charAt(i));
            }
            return pass.toString();
        }
     
    }
    le résultat est sans appel 25 second pour la méthode native et 60 pour l'autre
    D'où ma question, d'où vient l'écart?
    Est ce juste un ralentissement du à l'accès au donnée?

  2. #22
    Expert éminent
    Avatar de adiGuba
    Homme Profil pro
    Développeur Java/Web
    Inscrit en
    Avril 2002
    Messages
    13 938
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java/Web
    Secteur : Transports

    Informations forums :
    Inscription : Avril 2002
    Messages : 13 938
    Billets dans le blog
    1
    Par défaut
    Citation Envoyé par _xme_ Voir le message
    D'où ma question, d'où vient l'écart?
    Est ce juste un ralentissement du à l'accès au donnée?
    Comme je l'ai dit tu n'as pas d'accès direct aux attributs internes de la classe String. Ainsi dans le code suivant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
        public static int indexOf(String source, String str, int fromIndex) {
            return indexOf(source.toCharArray(), 0, source.length(), str
                    .toCharArray(), 0, str.length(), fromIndex);
        }
    Chaque appel à toCharArray() crée un nouveau tableau en mémoire, copie de celui utilisé en interne dans la classe String.

    Ainsi la différence vient du fait que tu instancies 10000000*2 objets temporaires...

    a++

    [edit] Et évites de parler de "fonctions natives". Ce terme n'est pas du tout adapté !!!

  3. #23
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    Alors d'abord, arrêter de dire "natif". Le code natif c'est du codé compilé à part et injecté dans la classe avec l'instruction "native".

    Ensuite dans ton cas, comme je l'ai dit plus haut, ici

    "return indexOf(source.toCharArray()"

    tu appel à chaque fois 2 x toCharArray, hors toCharArray va dupliquer le char[] présent à l'intérieur de la classe donc copies et allocation + travail à posteriori du garbage collector.Tu as donc à chaque itération création de deux objets d'objet, là ou quand tu appelle String.indexOf() rien n'est créé.

    Si je corrige correctement ton code
    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
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
     
    public class Main {
        private static int tokenNumber = 12;
        private static int tokenLength = 12;
     
        public static void main(String[] args) {
            // declaration
            StringBuilder chaineB = new StringBuilder();
            Long[] resultIndexOfNatif = new Long[tokenNumber + 1];
            Long[] resultIndexOf = new Long[tokenNumber + 1];
            String[] str = new String[tokenNumber];
            // generate chaine and str[]
            for (int j = 0; j < tokenNumber; j++) {
                str[j] = generateString(tokenLength);
                chaineB.append(str[j]);
                chaineB.append(" ");
            }
            System.out.println(chaineB);
            String chaine = chaineB.toString();
            // Chronometer IndexOfNatif
            long top = System.currentTimeMillis();
            for (int j = 0; j < tokenNumber; j++) {
                long tmp = System.currentTimeMillis();
                callIndexOfNatif(chaine, str[j]);
                resultIndexOfNatif[j] = System.currentTimeMillis() - tmp;
            }
            resultIndexOfNatif[tokenNumber] = System.currentTimeMillis() - top;
            // print result
            System.out.println("Resultat IndexOfNatif : ");
            for (int j = 0; j < tokenNumber + 1; j++) {
                System.out.println(j + " : " + resultIndexOfNatif[j]);
            }
            // Chronometer IndexOf recodé
            long top2 = System.currentTimeMillis();
            for (int j = 0; j < tokenNumber; j++) {
                long tmp = System.currentTimeMillis();
                callIndexOf(chaine, str[j]);
                resultIndexOf[j] = System.currentTimeMillis() - tmp;
            }
            resultIndexOf[tokenNumber] = System.currentTimeMillis() - top2;
            // print result
            System.out.println("Resultat IndexOf recodé : ");
            for (int j = 0; j < tokenNumber + 1; j++) {
                System.out.println(j + " : " + resultIndexOf[j]);
            }
        }
     
        private static void callIndexOf(String chaine, String string) {
            char[] x = chaine.toCharArray();
            char[] y = string.toCharArray();
            for (int i = 0; i < 10000000; i++) {
                indexOf(x, y);
            }
        }
     
        private static void callIndexOfNatif(String chaine, String string) {
            for (int i = 0; i < 10000000; i++) {
                chaine.indexOf(string);
            }
        }
     
        public static int indexOf(char[] source, char[] str) {
            return indexOf(source, str, 0);
        }
     
        public static int indexOf(char[] source, char[] str, int fromIndex) {
            return indexOf(source, 0, source.length, str, 0, str.length, fromIndex);
        }
     
        static int indexOf(char[] source, int sourceOffset, int sourceCount,
                char[] target, int targetOffset, int targetCount, int fromIndex) {
            if (fromIndex >= sourceCount) {
                return (targetCount == 0 ? sourceCount : -1);
            }
            if (fromIndex < 0) {
                fromIndex = 0;
            }
            if (targetCount == 0) {
                return fromIndex;
            }
     
            char first = target[targetOffset];
            int max = sourceOffset + (sourceCount - targetCount);
     
            for (int i = sourceOffset + fromIndex; i <= max; i++) {
                /* Look for first character. */
                if (source[i] != first) {
                    while (++i <= max && source[i] != first)
                        ;
                }
     
                /* Found first character, now look at the rest of v2 */
                if (i <= max) {
                    int j = i + 1;
                    int end = j + targetCount - 1;
                    for (int k = targetOffset + 1; j < end
                            && source[j] == target[k]; j++, k++)
                        ;
     
                    if (j == end) {
                        /* Found whole string. */
                        return i - sourceOffset;
                    }
                }
            }
            return -1;
        }
     
        private static String chars = "abcdefghijklmnopqrstuvwxyz";
        private static int charLength = chars.length();
     
        public static String generateString(int length) {
            StringBuilder pass = new StringBuilder(charLength);
            for (int x = 0; x < length; x++) {
                int i = (int) (Math.random() * charLength);
                pass.append(chars.charAt(i));
            }
            return pass.toString();
        }
     
     
    }
    j'obtiens
    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
    Resultat IndexOfNatif : 
    0 : 340
    1 : 499
    2 : 686
    3 : 927
    4 : 891
    5 : 1474
    6 : 1937
    7 : 1141
    8 : 1557
    9 : 1422
    10 : 2377
    11 : 2375
    12 : 15626
    Resultat IndexOf recodé : 
    0 : 316
    1 : 483
    2 : 647
    3 : 886
    4 : 842
    5 : 1822
    6 : 1949
    7 : 1089
    8 : 1535
    9 : 1402
    10 : 2354
    11 : 2252
    12 : 15577

  4. #24
    Membre confirmé
    Inscrit en
    Juillet 2009
    Messages
    93
    Détails du profil
    Informations forums :
    Inscription : Juillet 2009
    Messages : 93
    Par défaut
    @adiGuba @tchize_
    merci pour ces explications parfaitement claires

    @tchize_
    merci pour la correction

+ Répondre à la discussion
Cette discussion est résolue.
Page 2 sur 2 PremièrePremière 12

Discussions similaires

  1. Optimisation de votre SGBDR et de vos requêtes...
    Par SQLpro dans le forum Langage SQL
    Réponses: 35
    Dernier message: 11/01/2013, 11h49
  2. jvm : désactiver optimisations
    Par SQUAL dans le forum Langage
    Réponses: 21
    Dernier message: 30/11/2010, 10h47
  3. [JVM]Optimiser la bibliothèque de la JVM ?
    Par Regis.C dans le forum Général Java
    Réponses: 5
    Dernier message: 17/08/2005, 10h54
  4. [JVM][OPTIONS][OPTIMISATION]pc dédié à Java
    Par narmataru dans le forum Général Java
    Réponses: 7
    Dernier message: 16/04/2003, 17h12
  5. [langage] Optimiser la lecture d'un fichier
    Par And_the_problem_is dans le forum Langage
    Réponses: 2
    Dernier message: 11/06/2002, 10h24

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo