Algorithms - Improving O(n)
Hello,
I have a fairly straightforward problem. I have a 'while' loop that traverses over 3000 array elements (our company's client emails and other information) and sends out emails. Because it takes time to go through this set, is there a way to improve the performance so traversing of the array would be more efficient?
Thank you,
Victor.
[370 byte] By [
vic_ska] at [2007-11-26 23:05:43]

# 1
Huh? Iterating over all the elements of an array with n elements takes O(n) operations. If you want it to be less, you'll have to skip some elements.
# 2
And besides, I think you will find that sending the e-mails takes 99.9999% of the time and moving from one list entry takes 0.0001% of the time. Or less.
# 3
It sounds like what you really want is a Thread working in the background. And on the GUI you could have a progress bar being updated for every email.
# 4
Thanks for the posts guys. I thought it would be hopeless. What I tried was replace a string array with List. Then, as the counter moves through each element, remove that element from the List and simultanously pass it as String (typecast it) to email sending because remove returns Object.
The idea of this is to reduce the size of the original list stored in memory at each iteration. If I can't totally optimize O(n) at least I can free up some heap space along the way. Will this do the trick?
Thank you,
Victor.
# 5
As others have already said: the time it takes to
send the emails is likely taking up 99.999% of your time.
The list vs array is probably only taking up 0.001% of your time.
So any optimization on the list vs array is useless.
The fact that you're sending lots of emails is why
the application is slow.
at least I can free up some heap space along the way.
Will this do the trick?
The memory taken by the email address in your original
array will not be freed by the JVM (since the email-sender
will still need that email address for sending the email).
So removing it from the original list will not reduce
your memory usage in any significant way.
# 6
moves through each element, remove that element from the List and
Sounds like you're turning an O(n) into an O(n^2).
When you remove the element from the beginning of the list, it is going to shift all elements after the removed element to fill the gap.
You should start from the end of the list, traversing to the front.
# 7
You should start from the end of the list, traversing
to the front.
Thank you for replying. I think I am doing this:
while (counter >= 0){
MimeMessage message = new MimeMessage(mailSession);
message.addHeader("X-Confirm-Reading-To:", "me@gmail.com");
//
message.setContent(mp0);
message.setSubject(strSubject);
message.setFrom(fromAddr);
//**** Go Through List in my MySQLData class ****
emailTo = data.getEmail(counter);
toAddr = InternetAddress.parse(emailTo, false);
transport = mailSession.getTransport(toAddr[0]);
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(emailTo));
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
message = null;
counter--;
}
My MySQLData class
// *** Load strEmail List with elements from database then use getEmail()// To remove/return List elements
public String getEmail(int i)
{
return (String)strEmail.remove(i);
}
I certainly hope this is not O(n^2) ... is it?
Thank you,
Victor.
# 8
I certainly hope this is not O(n^2) ... is it?
It looks like you're doing it right.
run java with the -Xprof option to provide cpu info (or look at the -Xrunhprof:help information for more flexibility.
I guess most of the time is being spent in the transport.connect().
# 9
It looks like you're doing it right.
run java with the -Xprof option to provide cpu info
(or look at the -Xrunhprof:help information for more
flexibility.
I guess most of the time is being spent in the
transport.connect().
thanks
# 10
I guess most of the time is being spent in the
transport.connect().
In my environment, calling transport.connect() takes about 30 milliseconds. So instead of fiddling with trivia like you're doing, you could save a whole whack of time by only calling transport.connect() once.
# 11
can't you multi thread this process?split the number of mail addresses into equal sized blocks and assign to predetermined number of threads. serial processing has its limits my friend.
# 12
can't you multi thread this process?
split the number of mail addresses into equal sized
blocks and assign to predetermined number of threads.
serial processing has its limits my friend.
But then the computer will be making
multiple simultaneous connection to the email server.
It can be very disruptive to the network traffic.
For most ISP, they expect each client to only
have a reasonable number of simultaneous
SMTP/POP connections. If you suddenly start making
50 concurrent connections, they may think you're spamming.