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--;
}
}
}
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--;
}
}
}