From: Jérôme Benoit Date: Tue, 3 Apr 2018 13:33:00 +0000 (+0200) Subject: exo2: Add for real. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=b42baaebf13977077ead822d691d0787fe562a15;p=Project_POO.git exo2: Add for real. Signed-off-by: Jérôme Benoit --- diff --git a/exo2/Main.java b/exo2/Main.java new file mode 100644 index 0000000..85d48b5 --- /dev/null +++ b/exo2/Main.java @@ -0,0 +1,118 @@ +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.ListIterator; +import java.util.concurrent.TimeUnit; + +class Main { + + private static void addMiddle(ArrayList array, E value) { + int mid = array.size() / 2; + array.add(mid + 1, value); + } + + private static void addMiddle(LinkedList list, E value) { + int mid = list.size() / 2; + list.add(mid + 1, value); + } + + /** + * Should mimic the collection add(int index, T value) method + * @param array [description] + * @param value [description] + * @return [description] + */ + private static void addMiddleIter(ArrayList array, E value) { + int mid = array.size() / 2; + ListIterator iter = array.listIterator(); + int i = 0; + // go to the element at mid index + while (iter.hasNext() && i < mid) { + iter.next(); + i++; + } + iter.next(); // Insert before mid + 1 + iter.add(value); + + } + + /** + * Should mimic the collection add(int index, T value) method + * @param list [description] + * @param value [description] + * @return [description] + */ + private static void addMiddleIter(LinkedList list, E value) { + int mid = list.size() / 2; + ListIterator iter = list.listIterator(); + int i = 0; + // go to the element at mid index + while (iter.hasNext() && i < mid) { + iter.next(); + i++; + } + iter.next(); // Insert before mid + 1 + iter.add(value); + } + + private static void addNEMiddle(ArrayList array, int Nelements) { + ListIterator iter = array.listIterator(); + int i = 0; + // go to the element at the middle index + while (iter.hasNext() && i < array.size() / 2) { + iter.next(); + i++; + } + iter.next(); // Insert before mid + 1 + for (int j = 0; j < Nelements; j++) { + iter.add((E)new Object()); + } + } + + private static void addNEMiddle(LinkedList list, int Nelements) { + ListIterator iter = list.listIterator(); + int i = 0; + // go to the element at the middle index + while (iter.hasNext() && i < list.size() / 2) { + iter.next(); + i++; + } + iter.next(); // Insert before mid + 1 + for (int j = 0; j < Nelements; j++) { + iter.add((E)new Object()); + } + } + + /** + * The main() function + * @param String[] args main() function arguments array + */ + public static void main(String[] args) { + ArrayList array = new ArrayList(); + LinkedList list = new LinkedList(); + + for (int i = 0; i < 1000000; i++) { + array.add(i); + list.add(i); + } + + long startTime = System.nanoTime(); + //for (int i = 0; i < 100000; i++) { + // addMiddleIter(array, i); + //} + addNEMiddle(array, 100000); + long stopTime = System.nanoTime(); + long execTime = stopTime - startTime; + //System.out.println("Insert time in the middle of the ArrayList: " + TimeUnit.NANOSECONDS.toSeconds(execTime) + " s"); + System.out.println("Insert time in the middle of the ArrayList: " + execTime + " ns"); + + startTime = System.nanoTime(); + //for (int i = 0; i < 100000; i++) { + // addMiddleIter(list, i); + //} + addNEMiddle(list, 100000); + stopTime = System.nanoTime(); + execTime = stopTime - startTime; + //System.out.println("Insert time in the middle of the LinkedList: " + TimeUnit.NANOSECONDS.toSeconds(execTime) + " s"); + System.out.println("Insert time in the middle of the LinkedList: " + execTime + " ns"); + } +} diff --git a/exo2/Makefile b/exo2/Makefile new file mode 100644 index 0000000..b6e4a34 --- /dev/null +++ b/exo2/Makefile @@ -0,0 +1,89 @@ +# define compiler and compiler flag variables +# define a variable for compiler flags (JFLAGS) +# define a variable for the compiler (JC) +# define a variable for the Java Virtual Machine (JVM) + +JFLAGS = -g +JC = javac +JVM = java + +# +# Clear any default targets for building .class files from .java files; we +# will provide our own target entry to do this in this makefile. +# make has a set of default targets for different suffixes (like .c.o) +# Currently, clearing the default for .java.class is not necessary since +# make does not have a definition for this target, but later versions of +# make may, so it doesn't hurt to make sure that we clear any default +# definitions for these +# + +.SUFFIXES: .java .class + + +# +# Here is our target entry for creating .class files from .java files +# This is a target entry that uses the suffix rule syntax: +# DSTS: +# rule +# DSTS (Dependency Suffix Target Suffix) +# 'TS' is the suffix of the target file, 'DS' is the suffix of the dependency +# file, and 'rule' is the rule for building a target +# '$*' is a built-in macro that gets the basename of the current target +# Remember that there must be a < tab > before the command line ('rule') +# + +.java.class: + $(JC) $(JFLAGS) $*.java + + +# +# CLASSES is a macro consisting of N words (one for each java source file) +# When a single line is too long, use \ to split lines that then will be +# considered as a single line. For example: +# NAME = Camilo \ + Juan +# is understood as +# NAME = Camilo Juan + +CLASSES = \ + Main.java + +# +# MAIN is a variable with the name of the file containing the main method +# + +MAIN = Main + +# +# the default make target entry +# for this example it is the target classes + +default: classes + + +# Next line is a target dependency line +# This target entry uses Suffix Replacement within a macro: +# $(macroname:string1=string2) +# In the words in the macro named 'macroname' replace 'string1' with 'string2' +# Below we are replacing the suffix .java of all words in the macro CLASSES +# with the .class suffix +# + +classes: $(CLASSES:.java=.class) + + +# Next two lines contain a target for running the program +# Remember the tab in the second line. +# $(JMV) y $(MAIN) are replaced by their values + +run: $(MAIN).class + $(JVM) $(MAIN) + +# this line is to remove all unneeded files from +# the directory when we are finished executing(saves space) +# and "cleans up" the directory of unneeded .class files +# RM is a predefined macro in make (RM = rm -f) +# + +clean: + $(RM) *.class diff --git a/exo2/comments b/exo2/comments new file mode 100644 index 0000000..c677b5a --- /dev/null +++ b/exo2/comments @@ -0,0 +1 @@ +La liste chaînée est plus efficace pour l'insertion en son milieu avec un grand nombre d'éléments.