code optmization

M

mank

Following java code is executed many multiple times in my program.
Any ideas to optmize it?

if (result.group(2)== null || result.group(2).trim().length() < 1)
return var;
}else {
var=String.valueOf(Math.round(Float.parseFloat(var)* 3.0));
}
if (result.group(1) != null && result.group(1).trim().length() > 0) {
return result.group(1) + result.group(2);
}else if (result.group(3) != null && result.group(3).trim().length() > 0) {
return result.group(2) + result.group(3);
}else {
return "$"+ result.group(2);
}

Thanks
 
W

Will Hartung

mank said:
Following java code is executed many multiple times in my program.
Any ideas to optmize it?

if (result.group(2)== null || result.group(2).trim().length() < 1)
return var;
}else {
var=String.valueOf(Math.round(Float.parseFloat(var)* 3.0));
}
if (result.group(1) != null && result.group(1).trim().length() > 0) {
return result.group(1) + result.group(2);
}else if (result.group(3) != null && result.group(3).trim().length() > 0) {
return result.group(2) + result.group(3);
}else {
return "$"+ result.group(2);
}

Thanks

Going solely with what's presented, here ya go.

String resultGroup2 = result.group(2);
if (resultGroup2 == null || resultGroup2.trim().length() < 1) {
return var;
}
String resultGroup1 = result.group(1);
if (resultGroup1 != null && resultGroup1.trim().length() > 0) {
return resultGroup1 + resultGroup2;
} else {
String resultGroup3 = result.group(3);
if (resultGroup3 != null && resultGroup3.trim().length() > 0) {
return resultGroup2 + resultGroup3;
} else {
return "$"+ resultGroup2;
}
}


Since I don't know how expensive "result.group(1)" method call is, I cached
the intermediate results.

Since 'var' is not used outside of the first IF block, I eliminated it
completely from the else. An aggressive compile might have well eliminated
that automatically already. parseFloat tends to be an expensive operation in
most any language.

Other than that, without knowing much more than what you've supplied, I'd
argue that my changes will have as much effect on performance as shortening
the network cables between the tiers.

Regards,

Will Hartung
([email protected])
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

mank said:
Following java code is executed many multiple times in my program.
Any ideas to optmize it?

if (result.group(2)== null || result.group(2).trim().length() < 1)
return var;
}else {
var=String.valueOf(Math.round(Float.parseFloat(var)* 3.0));
}
if (result.group(1) != null && result.group(1).trim().length() > 0) {
return result.group(1) + result.group(2);
}else if (result.group(3) != null && result.group(3).trim().length() > 0) {
return result.group(2) + result.group(3);
}else {
return "$"+ result.group(2);
}

There are a lot of common subexpressions to be pulled out of the above
code. Factor out the calls to group. If you have a good idea of which
branches are likely to be taken and which are not, you can try to
reorder the tests (assuming the order does not matter) to reduce bad
branch predictions. The assignment to var is also a dead assignment in
your example. It is hard to be more specific since the example is so
short, and taken out of context.

(Insert obligatory warning about premature optimization. How many times
is many multiple times? Have you profiled your app to make certain this
is a bottle neck?)
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Will said:
Since 'var' is not used outside of the first IF block, I eliminated it
completely from the else. An aggressive compile might have well eliminated
that automatically already. parseFloat tends to be an expensive operation in
most any language.

Actually, a compiler that eliminated the call to parseFloat would be
dead wrong, since parseFloat can throw an exception and thus alter the
control flow in the program.
 
Y

Yu SONG

mank said:
Following java code is executed many multiple times in my program.
Any ideas to optmize it?

if (result.group(2)== null || result.group(2).trim().length() < 1)
return var;
}else {
var=String.valueOf(Math.round(Float.parseFloat(var)* 3.0));
}
if (result.group(1) != null && result.group(1).trim().length() > 0) {
return result.group(1) + result.group(2);
}else if (result.group(3) != null && result.group(3).trim().length() > 0) {
return result.group(2) + result.group(3);
}else {
return "$"+ result.group(2);
}

Thanks

If you can change the "group()" method, add a trim() method and some
other methods to deal with the length() (i.e. isGtOne) in that class.

--
Song

/* E-mail.c */
#define User "Yu.Song"
#define At '@'
#define Warwick "warwick.ac.uk"
int main() {
printf("Yu Song's E-mail: %s%c%s", User, At, Warwick);
return 0;}

Further Info. : http://www.dcs.warwick.ac.uk/~esubbn/
_______________________________________________________
 
C

Chris Smith

mank said:
Following java code is executed many multiple times in my program.
Any ideas to optmize it?

A number of people have taken guesses so far. You need to provide more
information, though, to get a good answer. For example, what is 'var'?
We don't even see a declaration. Will assumed it's a local variable,
while others assumed it's a field. If it is a field, then Will's advice
is dead wrong. Maybe it's time you gave more information.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

mank

1. public static String getData(String var,float mFactor) {
if (var == null || var.trim().length() == 0) {
return null;
}
try {
Pattern pattern = PatternManager.getDollarAmountPattern();
if (pattern==null) return var;
PatternMatcher matcher = new Perl5Matcher();
if (matcher.contains(var, pattern)) {
MatchResult result = matcher.getMatch();
var = result.group(2);
if (result.group(2) == null || result.group(2).trim().length() < 1)
return var;
else {
copay=String.valueOf(Math.round(Float.parseFloat(var)*mFactor));
}
if (result.group(1) != null && result.group(1).trim().length() > 0) {
return result.group(1) + var;
}
else if (result.group(3) != null && result.group(3).trim().length() >
0) {
return result.group(2) + result.group(3);
}
else {
return "$" + result.group(2);
}}}
catch (NumberFormatException ex) {}
return copay;
}
2. result.group(1/2/3) is a method used to extract the part of the
input pattern. It is a standard method of jakarta ORO (pattern
matching)..
3.My question was which is a better coding style if I have to call a
method thrice then should I use a temporary variable as is done in
your solution?

Thanks
 
B

Babu Kalakrishnan

[Moving top posted response down]
Chris Smith <[email protected]> wrote in message

1. public static String getData(String var,float mFactor) {
if (var == null || var.trim().length() == 0) {
return null;
}
try {
Pattern pattern = PatternManager.getDollarAmountPattern();
if (pattern==null) return var;
PatternMatcher matcher = new Perl5Matcher();
if (matcher.contains(var, pattern)) {
MatchResult result = matcher.getMatch();
var = result.group(2);
if (result.group(2) == null || result.group(2).trim().length() < 1)
return var;
else {
copay=String.valueOf(Math.round(Float.parseFloat(var)*mFactor));
}
if (result.group(1) != null && result.group(1).trim().length() > 0) {
return result.group(1) + var;
}
else if (result.group(3) != null && result.group(3).trim().length() >
0) {
return result.group(2) + result.group(3);
}
else {
return "$" + result.group(2);
}}}
catch (NumberFormatException ex) {}
return copay;
}
2. result.group(1/2/3) is a method used to extract the part of the
input pattern. It is a standard method of jakarta ORO (pattern
matching)..
3.My question was which is a better coding style if I have to call a
method thrice then should I use a temporary variable as is done in
your solution?

Please do not top-post. It is difficult to follow the thread.

The code you have posted this time adds something to the previous
version, but isn't still clear enough. The variable "var" is now known
to be local, but you've introduced a new one called "copay" which isn't
declared locally. So we have to assume that it is an instance variable -
Is this correct ?

There is only one suggestion I can provide - though that has more to do
with improving the readability of the code rather than optimization
(Though I suspect it might improve performance as well) :

When you use the same complex expression a multiple number of times in
any section of code, it is worthwhile to move it into a separate method.
In your code, the expression "someString != null &&
someString.trim().length() > 0" (and the negation of the same
expression) occurs in 4 places, so it is well worth it to move it into a
method.

Since you're using jakarta libraries anyway, you'll find that the same
function is available as "isNonBlank()" and the negated function as
"isBlank()" in the org.jakarta.commons.lang.StringUtils class. Also that
library implements it slightly more efficiently (checks for strings that
are all whitespace without producing an intermediate String object using
trim()).

And the answer to your point no. (3) : In my opinion it is always better
to use a temporary variable than calling a function more than once on
the fly. Introducing a variable only adds an additional reference slot
in your stack frame - whereas calling the method multiple number of
times will most probably create additional Objects as well - more work
for the garbage collector, as well as use up extra execution time (which
may matter if the method in question is complex).

Of course the above will apply only for method calls that will yield the
same result when called multiple number of times. Not for methods such
as iterator.next() :)

BK


BK
 
B

Babu Kalakrishnan

Babu said:
The code you have posted this time adds something to the previous
version, but isn't still clear enough. The variable "var" is now known
to be local, but you've introduced a new one called "copay" which isn't
declared locally. So we have to assume that it is an instance variable -
Is this correct ?

Just noticed that the method you've defined is declared static. So copay
needs to be static as well if not a local. Do you think this is correct ??

If it is local, then you might want to defer the String->float /
float->string conversions to the point where it is actually needed.

BK
 

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
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top