Performance Concerns

W

WJ

I have two quick questions.

1. I have a lot of places I need to convert Strings and ints back and
forth. I've read that creating an Integer object and doing a .parseInt or
..toString is not efficient, especially when doing it alot. Is there a
better way?
I'm working on switching the source to all one type, but I don't have
control of that.

2. I need to look up a value based on two keys, sort of like a map. So I'd
have values like
1 1 Value a
1 2 Value b
2 1 Value c
2 2 Value d
2 3 Value e

I thought of having a map of maps. Each one could have several entries and
I like the hash lookup. But the code seems inefficient to map.get("2"),
cast that as a map, then map.get("3"), to get the value I want.
is there a better way?


Thanks!
 
C

Christophe Vanfleteren

WJ said:
I have two quick questions.

1. I have a lot of places I need to convert Strings and ints back and
forth. I've read that creating an Integer object and doing a .parseInt or
.toString is not efficient, especially when doing it alot. Is there a
better way?
I'm working on switching the source to all one type, but I don't have
control of that.

If the number of int/strings is very limited, you could make a map with say,
the String representations of the first 100 numbers or something like that.
2. I need to look up a value based on two keys, sort of like a map. So
I'd have values like
1 1 Value a
1 2 Value b
2 1 Value c
2 2 Value d
2 3 Value e

I thought of having a map of maps. Each one could have several entries
and
I like the hash lookup. But the code seems inefficient to map.get("2"),
cast that as a map, then map.get("3"), to get the value I want.
is there a better way?
Just a tip:

never start to "optimize" your code unless you're sure that it is a
bottleneck. Always go for clarity and maintainability first.

As some very smart people have said before me:

"We should forget about small efficiencies, say about 97% of the time:
PrematureOptimization is the root of all evil." - Donald Knuth

Rules of Optimization:
Rule 1: Don't do it.
Rule 2 (for experts only): Don't do it yet.
- M.A. Jackson

So I'd suggest you actually test/profile your code and see if you will have
performance problems with your current code.
 
K

Karthik

WJ said:
I have two quick questions.

1. I have a lot of places I need to convert Strings and ints back and
forth. I've read that creating an Integer object and doing a .parseInt or
.toString is not efficient, especially when doing it alot. Is there a
better way?
I'm working on switching the source to all one type, but I don't have
control of that.

2. I need to look up a value based on two keys, sort of like a map. So I'd
have values like
1 1 Value a
1 2 Value b
2 1 Value c
2 2 Value d
2 3 Value e
May be the first two columns can be encapsulated as one object and
you can go ahead.

obj1 (1,1) Value a
obj2(1, 2) Value b

Don't forget to override the equals , compareTo method for the new
object created.
 
R

Roedy Green

I thought of having a map of maps. Each one could have several entries and
I like the hash lookup. But the code seems inefficient to map.get("2"),
cast that as a map, then map.get("3"), to get the value I want.
is there a better way?

If the keys are dense, you can have an arraylislt indexed by the first
number, and that gives you an arraylist. You then index it by the
second.

If they are not dense then you can make a combined key, cook up a hash
function and use a HashMap.

Your key objects need to implement equals and hashCode.

see http://mindprod.com/jgloss/hashtable.html
it could be an an object with two ints that returns the xor of the
two ints as the hashcode.

equals is defined as (other.a ==a) && (other.b == b)
 
W

WJ

Just a tip:
never start to "optimize" your code unless you're sure that it is a
bottleneck. Always go for clarity and maintainability first.

As some very smart people have said before me:

"We should forget about small efficiencies, say about 97% of the time:
PrematureOptimization is the root of all evil." - Donald Knuth

Rules of Optimization:
Rule 1: Don't do it.
Rule 2 (for experts only): Don't do it yet.
- M.A. Jackson

So I'd suggest you actually test/profile your code and see if you will have
performance problems with your current code.


Yeah, I know. My emphasis is functional. But I try to put a little effort
into making the code pretty. :p
 
W

WJ

May be the first two columns can be encapsulated as one object and
you can go ahead.

obj1 (1,1) Value a
obj2(1, 2) Value b

Don't forget to override the equals , compareTo method for the new
object created.

This is a great idea! I know the first two columns (they are actually
Strings) have a unique constraint
in the database, so that should go well.

Thanks!
 
R

Roedy Green

For some reason, I forgot that
the .toString(int i) method was static.

look inside. You will see it does everything with local variables on
the stack. If it needed intermediate values, that would be a problem.
 
C

Chris Smith

WJ said:
This is a great idea! I know the first two columns (they are actually
Strings) have a unique constraint
in the database, so that should go well.

It is a great idea, but don't forget to override hashCode -- you should
do that every time you override equals.

compareTo is optional, though, and only applies if there is a natural
ordering of your keys. I don't see one, but maybe with more semantic
knowledge of the problem, you do.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top