[trie value de HashMap] Soucis de performances
Bonsoir d'un débutant
Après pas mal de recherche sur le forum, j'ai bien trouvé des exemples avec compareTo mais je ne suis jamais arrivé à mettre ça en oeuvre. Du cou j'ai fait ma propre méthode mais niveau performance c'est une catastrophe. 400 000 IP en 8 min sachant que la conso se fait à 99,99% dans mon algo de trie pourri :calim2:
Si quelqu'un pouvait m'indiquer la ou les démarche(s) à suivre pour améliorer les codes suivant :
Code:
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
| import java.util.regex.*;
public class Main {
public static void main(String[] args) {
LireFichier fichier = null;
EcrireFichier cible = null;
Pattern p = Pattern.compile("SRC=(\\S+)");
try {
fichier = new LireFichier(args[0]);
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Nombre d'arguments incorrect");
System.exit(1);
}
Ip adresse = new Ip();
String lecture = "";
while((lecture = fichier.getLigne()) != null) {
Matcher m = p.matcher(lecture);
while(m.find())
adresse.addIp(m.group(1));
}
fichier.closeFichier();
System.out.println("lecture terminée");
adresse.trie();
System.out.println("Triage terminé");
try {
cible = new EcrireFichier(args[1]);
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Nombre d'arguments incorrect");
System.exit(1);
}
for(int i = 0; i < adresse.getSize(); ++i) {
cible.write(adresse.getIp(i) + " => " + adresse.getNb(i));
}
cible.close();
System.out.println("écriture terminée");
}
} |
Code:
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
| import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
public class EcrireFichier {
private String nomFichier = "";
private BufferedWriter bw = null;
public EcrireFichier(String arg) {
this.setNomFichier(arg);
try {
bw = new BufferedWriter(new FileWriter(nomFichier));
}
catch(FileNotFoundException e) {
System.out.println("Fichier introuvable");
System.exit(1);
}
catch(IOException e) {
System.out.println("Erreur d'E/S");
System.exit(1);
}
}
public void write(String ligne) {
try {
bw.write(ligne + "\n");
}
catch(IOException e) {
System.out.println("Erreur d'E/S");
System.exit(1);
}
}
public void setNomFichier(String nomFichier) {
this.nomFichier = nomFichier;
}
public void close() {
if(bw != null) {
try {
bw.flush();
bw.close();
}
catch(IOException e) {
System.out.println("Erreur d'E/S");
System.exit(1);
}
}
}
} |
Code:
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
| import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileNotFoundException;
public class LireFichier {
private String nomFichier = "";
private BufferedReader rd = null;
// Constructeur
public LireFichier(String arg) {
this.setNomFichier(arg);
try {
rd = new BufferedReader(new FileReader(nomFichier));
}
catch(FileNotFoundException e) {
System.out.println("Fichier introuvable");
System.exit(1);
}
catch(IOException e) {
System.out.println("Erreur d'E/S");
System.exit(1);
}
}
// Fermeture du fichier
public void closeFichier() {
try {
rd.close();
}
catch(IOException e) {
System.out.println("Erreur d'E/S");
System.exit(1);
}
}
// Accesseurs et mutateurs
public String getNomFichier() {
return this.nomFichier;
}
public void setNomFichier(String nomFichier) {
this.nomFichier = nomFichier;
}
public String getLigne() {
String str = "";
try {
str = rd.readLine();
}
catch(IOException e) {
System.out.println("Erreur d'E/S");
System.exit(1);
}
return str;
}
} |
Code:
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
| import java.util.HashMap;
import java.util.TreeMap;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Set;
import java.util.Iterator;
public class Ip {
private HashMap<String, Integer> hm = new HashMap<String, Integer>();
private String[] tableauIp;
private int[] tableauNombre;
public void addIp(String ip) {
if(hm.containsKey(ip)) {
hm.put(ip, (hm.get(ip) + 1));
}
else {
hm.put(ip, 1);
}
}
public void trie() {
tableauIp = new String[hm.size()];
tableauNombre = new int[hm.size()];
int emplacementTab = 0;
Collection<Integer> valeur = hm.values();
List<Integer> liste = new ArrayList<Integer>(valeur);
Collections.sort(liste);
for(int i = (liste.size() - 1), j = 0; i >= 0; --i, ++j) {
Set e = hm.keySet();
Iterator clef = e.iterator();
Boolean trouve = false;
while(clef.hasNext() && !trouve) {
String key = (String)clef.next();
if(hm.get(key) == liste.get(i)) {
trouve = true;
tableauIp[j] = key;
tableauNombre[j] = hm.get(key);
++emplacementTab;
hm.remove(key);
}
}
}
hm.clear();
}
public String getIp(int i) {
return tableauIp[i];
}
public int getNb(int i) {
return tableauNombre[i];
}
public int getSize() {
return tableauIp.length;
}
} |
Meilleur voeux pour cette nouvelle année :mouarf3: :yaisse2: