views:

717

answers:

2

I was wondering how expensive Java's string encoding conversion algorithms are, say, for a piece of text is in EBCDIC that needs to be converted to UTF-16, or for a similar conversion of a large file. Are there any benchmarks on the cost of this conversion? Benchmarks for multiple encodings would be better.

+1  A: 

This is an O(n) algorithm. The time it takes to execute will increase more or less linearly with the length of the string you're converting (though if you are converting millions of very short strings, the overhead of the function calls will add to this).

In almost all situations this won't be a bottleneck. You could probably, for example, convert 10 to 40 MB of text per second. I don't have actual benchmark data though.

thomasrutter
A: 

I suspect it's negligible. I would be more worried about the cost of allocating new String objects if you're converting thousands of Strings, or the allocation of huge byte arrays, if you're converting very large Strings. But even then only in extreme circumstances.

Brian Agnew