Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Optimization for IF looooops...

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a method which need to have lot of if loops, so the performance getting very slow for one application, I dont know how to optimize for that method, Please suggest some way for this method or give a soloution or refer some books or do some thing..... for my problem ...

public void SampleWritingToStream( HttpServletRequest request, HttpServletResponse response, String egUrl, GetMethod httpGet, egInfo egInfo )
{
response.setStatus( httpGet.getStatusCode() );
Header contentType = httpGet.getResponseHeader( HTTP_HEADER_CONTENT_TYPE );

if (contentType != null)
{
response.setContentType( contentType.getValue() );
}

try
{
InputStream input = httpGet.getResponseBodyAsStream();
OutputStream out = response.getOutputStream();

int ch;
String filename = new File( egUrl ).getName();
if (filename.contains( "asp" ))
{
StringBuilder content = new StringBuilder();
while ((ch = input.read()) != -1)
{
content.append( (char)ch );
}

String egContent = content.toString();
egContent = egContent.replaceAll( egInfo.getegDomain(), getegDomain + ":" + eg_PORT );

if (egContent.contains( "eg1.aspx?" ))
{
if (egUrl.contains( "eg2.aspx" ))
{
egContent = egContent.replaceAll( "src=\"eg1.aspx?", "http://" + getegDomain + ":" + eg_PORT + "/Folder1/remoteuifiles/eg1.aspx" );
}
else
{
egContent = egContent.replaceAll( "eg1.aspx?", "http://" + getegDomain + ":" + eg_PORT + "/Folder1/remoteuifiles/eg1.aspx" );
}
}
if (egContent.contains( "eg3.aspx?" ))
{
egContent = egContent.replaceAll( "eg3.aspx?", "http://" + getegDomain + ":" + eg_PORT + "/Folder1/remoteuifiles/eg3.aspx" );
}

if (egContent.contains( "eg4.aspx?" ))
{
egContent = egContent.replaceAll( "eg4.aspx?", "http://" + getegDomain + ":" + eg_PORT + "/Folder1/remoteuifiles/eg4.aspx" );
}

if (egContent.contains( "eg5.aspx?" ))
{
egContent = egContent.replaceAll( "eg5.aspx?", "http://" + getegDomain + ":" + eg_PORT + "/Folder1/remoteuifiles/eg5.aspx" );
}

if (egContent.contains( "eg6.aspx?" ))
{
egContent = egContent.replaceAll( "eg6.aspx?", "http://" + getegDomain + ":" + eg_PORT + "/Folder1/remoteuifiles/eg6.aspx" );
}
if (egContent.contains( "eg7.aspx?" ))
{
egContent = egContent.replaceAll( "eg7.aspx?", "http://" + getegDomain + ":" + eg_PORT + "/Folder1/remoteuifiles/eg7.aspx" );
}

if (egContent.contains( "href=\"$STYLESHEET$\"" ))
{
egContent = egContent.replaceAll( "href=\"$STYLESHEET$\"", "href=\"http://" + getegDomain + ":" + eg_PORT + "/Folder1/remoteuifiles/$STYLESHEET$\"" );
}

if (egContent.contains( "src='eg8.js" ))
{
egContent = egContent.replaceAll( "src='eg8.js", "src='http://" + getegDomain + ":" + eg_PORT + "/Folder1/remoteuifiles/eg8.js'" );
}
if (egContent.contains( "src='../" ))
{
egContent = egContent.replaceAll( "src='../", "src='http://" + getegDomain + ":" + eg_PORT + "/Folder1/" );
}

if (egContent.contains( "src=\"../" ))
{
egContent = egContent.replaceAll( "src=\"../", "src=\"http://" + getegDomain + ":" + eg_PORT + "/Folder1/" );
}
if (egContent.contains( "<body marginwidth=3 marginheight=3 class=\"bodyLessons\">" ))
{
egContent = egContent.replaceAll( "<div class=\"normalDiv\">" , "<br><center><IMG src='/egimages/exampleimage.gif'></center><br><br><br><br><br><br><br><br><br><br><div id=\"bodyBG1\">" );
}
out.write( egContent.getBytes() );
}
else
{
while ((ch = input.read()) != -1)
{
out.write( ch );
}
}
out.flush();
out.close();
input.close();
}
catch (Throwable e)
{
logger.debug( e.getMessage() );
}



Thanks

Shanmugam
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If statements (they are not loops, by the way) typically aren't slow at all. So it's probably something else that's slow. To really know what's causing the slowness and not optimize the wrong code, you should use a profiler.
 
Shanmugam nagaraj
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ilja Preuss
Thanks for the quick response,


To really know what's causing the slowness and not optimize the wrong code, you should use a profiler.

How to use this profiler can give me one hello world example for that?
Or Give some urls links to search

Thanks

Shanmugam
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are several profilers ( probably ) try the Sun website, enter profiling tools into the search box. Here is a link to get started:

http://java.sun.com/developer/technicalArticles/Programming/HPROF.html

Originally posted by Shanmugam nagaraj:
How to use this profiler can give me one hello world example for that?
Or Give some urls links to search

 
author & internet detective
Posts: 40797
829
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shanmugam,
If you don't want to use a profiler, you could add printlns to log the time of different parts (in a loop.) For example, how long does it take to just read the file? How long does the contains take? How about the replaceAll? Is it faster to merge the contains/replaceAll?
reply
    Bookmark Topic Watch Topic
  • New Topic