• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

calculate value or get from map?  RSS feed

 
Ranch Hand
Posts: 207
2
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am writing some code to get romanization of hangul.  I originally thought of just having a map or String array with the unicode decimal numbers pointing to the romanization because there aren't that many syllables, but I found some code on github getting the romanization by doing some simple math on the unicode decimal number.  Which method would be faster to use, by map/array or by calculation?

thanks!!
 
Marshal
Posts: 61690
192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know. It all depends how the indexing of the map is done and how long it takes to populate the Map. I suspect there might not be much difference either way, particularly if there are only a few hundred combinations. Please supply more details.

I think this question is too difficult for beginning, so I shall move it.
 
Bartender
Posts: 9486
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I prefer calculation when there's a clear mathematical relationship between two concepts. However, transliteration depends on two arbitrarily defined character sets without a mathematical relationship. Even if there's a fixed offset between all the characters you want to use, that's just coincidence.
 
Al Hobbs
Ranch Hand
Posts: 207
2
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,
There are technically 11,000 characters, but 2,300 characters cover 99.9% of the most used ones.  So for the map/array I would have a map that contains the hangul characters like,한, for the key and the transliteration as han.  I could also use the unicode decimal number to act as a key in a string array.  The shear number of syllables and setting up the map/array seems like it would be extremely tedious.  
The characters are made up letters sort of so you can create the transliteration without having to look anything up.  Right now I wrote the code for that.  For the most part it covers everything except maybe a few edge that I need to handle once I figure out how to determine them.

What I have now(calculation method) is this:

I made it go backwards through the string because I thought handling the edge cases might be easier. Once I figure out the grammar I can just add a helper method to sort it out.

I also wasn't sure about making the Stringbuilders global variables.  I made them global variables so they only have to be created one time instead of for every single character, and i think stringbuilder is threadsafe.
 
Stephan van Hulst
Bartender
Posts: 9486
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Al Hobbs wrote:I also wasn't sure about making the Stringbuilders global variables. I made them global variables so they only have to be created one time instead of for every single character, and i think stringbuilder is threadsafe.


No matter. It's ALWAYS a bad idea to have global variables that are mutable.
 
Al Hobbs
Ranch Hand
Posts: 207
2
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of clearing the stringbuilders should I just declare in them in the methods, even if it means declaring them hundreds of times potentially?
 
Stephan van Hulst
Bartender
Posts: 9486
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, why not? If you think that will decrease performance of your application even the tiniest significant bit, I challenge you to write a benchmark to demonstrate it.
 
Al Hobbs
Ranch Hand
Posts: 207
2
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I will probably change it then.  I don't really know how certain things will affect performance.  Do you think that would be the case in most cases where I should not global variables because declaring multiple times won't really affect performance?
 
Stephan van Hulst
Bartender
Posts: 9486
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think maybe you are confused about the word "global". A global variable is a variable that's declared as static, and can therefore be accessed from any place in your application that can see the class definition.

Your builders are not currently globally accessible. You need an instance of the class Romanizer to access them. That's not to say that it's not a bad idea to have those fields. You use instance fields to remember state between method calls. Your hangulToRoman() method is stateless: Its outcome doesn't depend on state changes between method calls, it only depends on the method input. If that's the case, then why does it access fields? Caching results you've calculated before may be a valid answer, but only after you've tested that your application is slower than what's in the functional requirements. Even then, caching data is only useful for expensive calculations. Creating a new StringBuilder (which needs to be empty anyway) is peanuts in the grand scheme of things.

First write implementations that solve the problem, without resorting to tricks to achieve what you believe might be a performance improvement. Developers often waste more time writing performance enhanced code, than will be actually saved by the enhanced version of the code.

In this particular case, I recommend that you change your firstConsonants (plural) vowels and lastConsonants (plural) into maps or lists. Also make them unmodifiable, and static. Get rid of the other instance fields. Now, you can make hangulToRoman() a pure function by making it static, because it doesn't depend on object state.

Here's an example of one such field:
 
Al Hobbs
Ranch Hand
Posts: 207
2
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, if i change those id be able to turn it into a stateless static utility class as it is now ignoring edge cases.  The problem is that for the edge cases that i need to handle, it needs to know parts of the previous syllable, which would require a cache for which i planned on using an instance field.  Would that ruin its chance of becoming a static class or is there another way to remember to safely remember the previous syllable and make it a static method?
 
Stephan van Hulst
Bartender
Posts: 9486
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, you might want to write a custom Collector to perform a mutable reduction:

In this case, all the intermediate state (the previous syllable seen) is encapsulated by the local Accumulation class inside the transliteratingHangulToRoman() method. All you have to do is implement the transliterateHangulSyllableToRoman() based on the current syllable (as an integer code point) and the previous syllable (also a code point, may be null if you're processing the first syllable of the string or if the previous code point wasn't Hangul).
 
Al Hobbs
Ranch Hand
Posts: 207
2
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks really great.  Im thinking that the accumulator will automatically put each character into the accumulate method as an integer.  Im assuming that it would go left to right but i need the know the syllable on the right.  Is there a way to change iteration direction?  For capitalizing im guessing i would just capitalize the return of the public method.
 
Stephan van Hulst
Bartender
Posts: 9486
184
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Al Hobbs
Ranch Hand
Posts: 207
2
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!