Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In .NET, <code>Substring</code> is O(n) rather than the O(1) of Java. This is because in .NET, the String object contains all the actual character data itself<sup>1</sup> - so taking a substring involves copying all the data within the new substring. In Java, <code>substring</code> can just create a new object referring to the original char array, with a different starting index and length.</p> <p>There are pros and cons of each approach:</p> <ul> <li>.NET's approach has better cache coherency, creates fewer objects<sup>2</sup>, and avoids the situation where one small substring prevents a very large <code>char[]</code> being garbage collected. I believe in some cases it can make interop very easy too, internally.</li> <li>Java's approach makes taking a substring very efficient, and probably some other operations too</li> </ul> <p>There's a little more detail in my <a href="http://csharpindepth.com/Articles/General/Strings.aspx" rel="noreferrer">strings article</a>.</p> <p>As for the general question of avoiding performance pitfalls, I think I should have a canned answer ready to cut and paste: make sure your <em>architecture</em> is efficient, and implement it in the most readable way you can. Measure the performance, and optimise where you find bottlenecks.</p> <hr> <p><sup>1</sup> Incidentally, this makes <code>string</code> very special - it's the only non-array type whose memory footprint varies by instance within the same CLR.</p> <p><sup>2</sup> For small strings, this is a big win. It's bad enough that there's all the overhead of <em>one</em> object, but when there's an extra array involved as well, a single-character string could take around 36 bytes in Java. (That's a "finger-in-the-air" number - I can't remember the exact object overheads. It will also depend on the VM you're using.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload