performance string, stringbuffer, and stringtoken

M

Mike mmccaws

Just trying to learn about manipulating strings and tried to check
performance of doing a task in each of the string technologies string,
stringbuffer, stringtokenizer. Code pasted below

The questions are:

1.when using the utility System.currentTimeMillis(), I often have a
result of 0. Does this mean that when I have a value greater than 0
that there is some background operation that is swapping out with the
processor? Or is there something wrong with my code.

2. From the code included, the STtoken routine often takes less time
eventhough it appears to me to be uglier code. When I say it often
takes less time, it is when I'm running the code from a command prompt
window and the string prints to the console.(XP OS) From the code, am
I testing the performance correctly? if not what should I do.

3. What am I doing poorly in how I use string stringbuffer and
stringtokenizer?.

Thanks for your help

Mike (e-mail address removed)
Code:


/*
* Class.java
*
* Created on July 4, 2003, 6:30 PM
*/

/**
*
* @author Mike
*/
import java.util.*;

public class STcompare{


public void STstring(int i, String st)
{
/**public static void main (String[] args)
{ */

int firstposition = 0;
int lastposition = 0;

long start1, end1;


start1 = System.currentTimeMillis();


firstposition = st.indexOf(" ");
lastposition = st.lastIndexOf(" ");
String stfirst = st.substring(0,firstposition);
String stlast = st.substring(lastposition +1); //lastposition is
space add 1 for first character.
stlast = Character.toUpperCase(stlast.charAt(0)) +
stlast.substring(1);
String sttemp = st.substring((firstposition + 1), lastposition);
stlast = stlast.concat(" " + sttemp.concat(" " +
stfirst.toLowerCase()));

//System.out.println("original string : " + st);
System.out.println("final string: " + stlast);
end1 = System.currentTimeMillis();
long final1 = end1-start1;
System.out.println(i + "th count. Time STstring compare:
"+(final1));
// }
}






/** Creates a new instance of Class */

public void STbuffer(int i, String st)
{
/** public static void main (String[] args)
{ */



long start2 = System.currentTimeMillis();

StringBuffer stbuff = new StringBuffer(st);

//int firstspace = 0;
int lastspace = 0;
String space = " ";
int firstspace = stbuff.indexOf(" ");
lastspace = stbuff.lastIndexOf(" ");
StringBuffer stfirst = new StringBuffer(" " +
stbuff.substring(0, firstspace));


StringBuffer stlast = new
StringBuffer(stbuff.substring(lastspace + 1, stbuff.length()) + " ");

stbuff = stbuff.delete(lastspace, stbuff.length());
stbuff = stbuff.delete(0, firstspace + 1);

stbuff = stlast.append(stbuff);
stbuff = stbuff.append(stfirst.toString().toLowerCase());
//System.out.println( "new stbuff =: " + st);
System.out.println( "new stbuff =: " + stbuff.toString());
long end2 = System.currentTimeMillis();
System.out.println("Time STbuffer compare: " + (end2-start2));
// }
}


//import java.util.*;

public void STtoken(int i, String st)
{
/** public static void main (String[] args)
{ */
//Hello World of Java

String stfirst, second;
long start3 = System.currentTimeMillis();

String stlast = "";
int stlength = st.length();
int count = 0;
StringTokenizer sttoken = new StringTokenizer(st);
int tokencount = sttoken.countTokens();
StringBuffer buildst = new StringBuffer(st.length());

System.out.println("How many tokens? " + tokencount);
stfirst = sttoken.nextToken();
count++;
while (sttoken.hasMoreElements())
{
stlast = sttoken.nextToken();
if (sttoken.hasMoreElements())
{
buildst= buildst.append(" ");
buildst=buildst.append(stlast);
}
}

stlast = Character.toUpperCase(stlast.charAt(0)) +
stlast.substring(1);
stfirst = Character.toLowerCase(stfirst.charAt(0)) +
stfirst.substring(1);
buildst = buildst.append(" ");
buildst = buildst.append(stfirst);

buildst = buildst.insert(0, stlast);
String stfinal = new String(buildst.toString());
System.out.println("final : " + stfinal);
long end3 = System.currentTimeMillis();
System.out.println("Time STtoken compare: "+(end3-start3));
//}
}
public static void main (String[] args)
{
int i = 10;
String st = "No national political candidate would dare
think of running a campaign today without using the Internet, but none
of them is apparently using the Web as effectively right now as
Democratic presidential hopeful Howard Dean of Vermont"
+ "On Monday, Deans campaign raised an amazing $802083 online in one
day, pushing his fund raising above 7 million for the quarter that
began April 1 and putting him in the top tier of candidates seeking
the Democratic presidential nomination"
+ "The amount of money his campaign brought in online, and his use of
the Web to draw in supporters, keep them involved and organize them
locally, is winning plaudits from analysts and others who say his is
the first candidacy to put the Internet to full use"
+ "Hes really leveraging the medium, putting it in the hands of the
people, said Carol Baroudi, an analyst at Baroudi Bloor in Arlington
Mass and a co-author of The Internet for Dummies This medium is the
most dynamic medium available Its when I want it, its there, and it
keeps getting refreshed"
+ "The Dean Web site can be shared by people who can voice their
opinions and connect with others both online and offline, she said,
adding, What hes done is substantiated a community"
+ "The Web sites home page urges supporters to click to stay
connected, to get involved and to contribute. It also includes an
official Web log to keep supporters informed about campaign activities
and to provide easy access to local Dean meet up groups that can work
on the campaign locally As with other presidential campaign Web sites,
theres information about the candidate, from his background as a
physician to his term as governor in Vermont. And there are position
papers press releases and other traditional means of helping voters
learn more"
+ "But in something like a presidential campaign site, design matters,
and Deans designers got it right, Baroudi said. Its very inviting. I
think they did a stunning job. For one thing I can find everything I
want to find she said"
+ "To me, this is hopeful because it has a way of reaching out and
touching people in a way that cant be controlled yet, Baroudi said.
[President] Bush has been putting all of his money into owning the
media, and this is a medium he doesnt own."
+ "Nicco Mele, the webmaster for the Burlington, Vt.-based Dean
campaign, said the site was designed to be an extension and expression
of the candidate and to help people become personally involved in what
has been dubbed a people-powered campaign."
+ "The site is run using open-source software and is hosted in several
parts. The campaign blog is hosted by Rackspace Managed Hosting, while
content management and credit card processing services are provided
through Convio Inc. in Austin. Meetup Inc. in New York provides
meeting organization, and TVTonic.com helps send video clips of Deans
campaign to volunteers around the country so they can be shown to new
campaign supporters."
+ "Were really a low-budget campaign, and the TVTonic site provides a
cheap means of distributing video, compared with traditional
DVD-burning and overnight delivery, Mele said."
+ "Were getting obscene traffic for a political site, with more than
100,000 unique hits daily, Mele said. Although thats nothing compared
with major corporate sites, he said, that could change. Im planning on
it, he said."
+ "All the other Democratic candidates have Web sites, including U.S.
Rep. Richard Gephardt of Missouri, Sen. John Kerry of Massachusetts,
Sen. Joseph Lieberman of Connecticut and Sen. John Edwards of North
Carolina. But none seems to exude the excitement of the Dean site,
experts said."
+ "Its tapping into a sense of community, said Harry Wolhandler, an
analyst at Accelara Research in Harrisville, N.H. Theyve done a lot of
things right without having to be sophisticated about it."
+ "Most notable, Wolhandler said, is that the Dean site is direct in
getting its message across and connecting directly with viewers. Their
whole approach is to be connected, he said. It comes through as very
authentic."
+ "The site has calls to action all over it, from asking people to
join the campaign to asking for contributions for what has so far been
a very successful fund-raising effort. Hes lighting people up with it,
Wolhandler said."
+ "G. Terry Madonna, a political pollster and the director of the
Center for Politics and Public Affairs at Millersville University in
Millersville, Pa., said the Dean site communicates a clear message ...
that resonates with a core set of Democratic voters in the liberal
wing of the Democratic Party, which was the most upset over the
outcome of the 2000 election and over the war in Iraq."
+ "Hes been able to tap into that core constituency better than anyone
else, Madonna said."
+ "As engaging as Deans Web site is, Madonna said, it isnt enough to
make or break an election. But his online fund-raising success does
show that that situation could be changing."
+ "There isnt any doubt about it, these Web sites are critical in
terms of fund raising, critical in terms of massing an activist
nucleus to help the campaign, Madonna said. While I wouldnt say that
they can win an election, they can provide you with the tools to help
win. You could see it coming.";

STcompare tt = new STcompare();
while (i > 0)
{

tt.STstring(i, st);
tt.STtoken(i, st);
tt.STbuffer(i, st);
i--;
}


}
}
 
C

Chris Smith

Mike said:
The questions are:

1.when using the utility System.currentTimeMillis(), I often have a
result of 0. Does this mean that when I have a value greater than 0
that there is some background operation that is swapping out with the
processor? Or is there something wrong with my code.

Do you mean that you call System.currentTimeMillis and get back zero, or
that you call it twice and get the same number (so that the difference
is zero)? I'll bet it's the latter.

Two things:

a. Operating systems all have a maximum timer resolution, beyond which
you cannot determine the time more accurately (or it would be difficult
to do so, anyway). That's generally a lot longer than a millisecond.
So, while currentTimeMillis returns a number in milliseconds, it is NOT
accurate to the nearest millisecond. You might see it jumping in
increments of 40, for example. Depends on your hardware and operating
system.

b. Yes, currentTimeMillis does measure wall time, not CPU time.
Unfortunately, it's difficult to actually measure CPU time for a
specific given segment of Java code. In fact, you can't do so portably,
though you could write non-portable native code to do so. Really,
unless you're really gung-ho about perfect results, it's best to just
stop as many background processes as you can and try not to do much else
at the same time as your benchmarking. Even if you COULD measure CPU
time, that would still be desirable, since caching behavior is still
affected by background processes.

To deal with the zero number, your best bet is to run each test in a
large number of repetitions. Divide the total time by the number of
repetitions, and you've got your time.
2. From the code included, the STtoken routine often takes less time
eventhough it appears to me to be uglier code. When I say it often
takes less time, it is when I'm running the code from a command prompt
window and the string prints to the console.(XP OS) From the code, am
I testing the performance correctly? if not what should I do.

Looks like you're measuring the time fine... but your tests are doing a
lot of aparently unrelated tasks, that I can't make heads or tails of.
Generally, you'd measure the performance of one algorithm to accomplish
one specific task.
3. What am I doing poorly in how I use string stringbuffer and
stringtokenizer?.

I can't say without some kind of meaning to your code... and there is
none.

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

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

Jon A. Cruz

Roedy said:
I have never seen it give the wrong time. If you subtract two times
they may be less than a millisecond apart. On some systems, the clock
in not updated every millisecond.

Many VM's on MS Windows used the standard timer resolution of 55ms.

Mainly Win9x variants are limited to about that, whereas NT variants
have a timer that defaults to about 10ms. On my same machine running
Linux, I get a resolution of 1ms.
 
M

Mike mmccaws

Thanks everyone.

if I understand you correctly to get a better idea of the performance
I should be running the routing through a string maybe 10,000 or
100,000 words long. Or loop through the the same method 100 times
starting the time process before and afterward and not for each access
to the method.

By the way, the only thing the routine does is swap the first and last
word, ignoring punctuation, and make sure the first word's first
character is uppercase and the last word is lowercase. Just a string
exercise. I'm writing several different string manipulation routines,
each trying to use string, stringbuffer, and stringtokenizer types to
learn what can be done, and hopefully what should be done. Learning
Java is going to force me to learn how to actually program as appose
to cut and paste code, a good thing.

Mike
 

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,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top