• Post Reply Bookmark Topic Watch Topic
  • New Topic

more help with performance classes..  RSS feed

 
Rebecca Plumb
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm still having trouble measuring the performance of ArrayList, Vector, LinkedList by inserting 100,000 Strings and then deleting them one by one. Use a Calendar Object to measure the time in milliseconds:

ArrayList:


import java.util.Date;

public class ArrayList<String> {
/**
* @param args
*/
public static void main(String[] args) {

Date d = new Date();

ArrayList<String> list = new ArrayList<String>();

for (int i=0; i<10000000; i++);

int i = 0;{

String string = (String) Integer.toString (i);
toString();
string = null;
string.add(myString);

Date e = new Date();

System.out.println("Time:" + (e.getTime()-d.getTime()));
}
}
private static void getTime() {
}
private void add(String myString) {
}
}
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try using the CODE tag, you can edit your post to modify it,

also



be careful with the semicolons in other places!
(in a hurry - or i'd say more)
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I warned in my previous post, writing your own classes with the same name as common classes in the API can lead to serious confusion later. I'm pretty sure the intention here is to use the ArrayList already defined in java.util and not write your own ArrayList class.

I think a good approach would be to describe the steps you want to follow in English first -- before trying to write the code. Once you have a clear idea of what you're trying to do, it will be much easier to code. For example, can you explain what's supposed to be happening in these lines...?
String string = (String) Integer.toString (i);
toString();
string = null;
string.add(myString);
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, as Bill suggested, please use Code Tags to keep your indentation. You can edit your posts by clicking on the paper/pencil icon.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
... For example, can you explain what's supposed to be happening in these lines...?

I was asking the question to prompt you to look at that code and consider what it's really doing. The code won't work. But more importantly, I'm not sure what your thought process was with those lines, and that's why I suggested describing this in English -- getting the ideas straight -- before coding.
String myString = Integer.toString (i);
toString ();
Vector vector = null;
vector.add(myString);

The 2nd and 3rd lines will cause trouble. Your class does not define a toString() method, so trying to call this by itself will result in a compilation error. Even if you had such a method and it returned a String, you're not doing anything with that String, so there doesn't seem to be any point in calling it. Besides, you've already gotten the String you need on the 1st line. The 3rd line sets "vector" to null, so when you try to do something with it on the 4th line (calling "add" on a null reference), you will get a runtime exception.

Actually, the 1st and 4th lines alone will accomplish what you want, and you could even simplify this as a single line...

vector.add(Integer.toString(i));

Taking shots in the dark with code will only lead to frustration. Make sure you have a clear idea of what you're doing with each line. Tell us what you're thinking, and we'll let you know if it makes sense.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a lot of pieces to this, so I suggest breaking this down into smaller parts.

For example, start by writing code that just adds 100,000 Strings to a Vector. That's all. Don't worry about the Calendar instance, and don't worry about removing the Strings. Show us how that works, and we'll get you to the next step.
 
Rebecca Plumb
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
There are a lot of pieces to this, so I suggest breaking this down into smaller parts.

For example, start by writing code that just adds 100,000 Strings to a Vector. That's all. Don't worry about the Calendar instance, and don't worry about removing the Strings. Show us how that works, and we'll get you to the next step.


Ok, I was able to figure it out. The person who was orginally helping me, confused me even more than I needed to be. This was alot simpler than I realized:

import java.util.Date;
import java.util.LinkedList;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {

Date d = new Date();

String s = "test";


LinkedList<String> list = new LinkedList<String>();

for (int i=0; i<10000000; i++);
list.add(s);

Date e = new Date();
System.out.println("Time:" + (e.getTime()-d.getTime()));
}
}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rebecca Plumb:
... Ok, I was able to figure it out. The person who was orginally helping me, confused me even more than I needed to be. This was alot simpler than I realized...

Cool!

Now you just need to remove these Strings too.

(One other detail: It looks like the requirements were to use a Calendar instance for the time -- not a Date instance.)
 
Mark Newton
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
Now you just need to remove these Strings too.


...and to UseCodeTags - it's really not hard.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!