• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I improve this piece of code?  RSS feed

 
v ray
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This part of code will run around 30 million times, and I am not sure if I have used Set, Map and Iterator properly enough. The code works just fine, but I am looking to improve it.

Regards.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have multiple rows with the same "id"? If so, that goes kind of against the concept of an ID (which, after all, stands for "identity"). If there's more than one object with the same ID it's not really an ID. But that's just terminology.

Plus, if the "id" are all different, then "hm" would contain 30 million elements, and you would have executed the println statement around 450 trillion times (30 million times 15 million) - surely that can't be what's happening?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a missing closing brace ("}") somewhere? This loops over the result set, adding one entry to a map for each database row, and then (again, once for each row) loops over the whole map, displaying all its contents. I can't imagine why you'd do that.

Also, unless the chunk of XML in the Clob is quite small, reading it one character at a time is quite inefficient. A loop that uses read(char[], int, int) would probably be much better.
 
v ray
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No ofcourse not, I am not printing the contents 30 million times. The idea is to write the records to a linked list and periodically write it to a dump file when the size of the linked list exceeds a certain limit.


Do you have multiple rows with the same "id"? If so, that goes kind of against the concept of an ID (which, after all, stands for "identity"). If there's more than one object with the same ID it's not really an ID. But that's just terminology.

No, each Id is unique, why would you assume the ids are the same or not unique?

I think choosing the hashmap was a bad idea, so instead, I am doing this:






Also, unless the chunk of XML in the Clob is quite small, reading it one character at a time is quite inefficient. A loop that uses read(char[], int, int) would probably be much better.



Yes, it is pretty large, but I cant think of the appropriate conditional statement.

Clob clob = rs.getClob(2);
InputStream is = clob.getAsciiStream();
while(is.read > 0)
{
//what do I do??
}
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you want something like:
 
v ray
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no read(char,int,int) I think.
This is what we have:

read(byte[] b, int off, int len)
Reads up to len bytes of data from the input stream into an array of bytes.

how about this?

 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by v ray:
There is no read(char,int,int) I think.



Ok, I looked up Clob, it also has a getCharacterStream() method, which gives you a Reader instead of an InputStream.
Is it an option to use that one?
 
v ray
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, doesnt really matter to me, the basic objective of the program is to read from the database, write it to a collection which then writes to a load file.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!