It is, I hope, well-known that naïve string concatenation in a loop is a quadratic “Schlemiel” algorithm. (If you haven’t read Joel’s essay, stop reading this and go read it now.)
Monthly Archives: June 2013
A strange rainbow
Good morning and happy solstice everyone; no computer stuff today. On today’s fun-for-Friday FAIC, here’s a photo I snapped last weekend of the strangest set of rainbows (well, technically these are not “rainbows” because it was not raining, but, whatever) I have ever seen in my life. (Click for a larger version.)
The photo was taken on the 15th of June at 7:30 PM Pacific Daylight Time in Seattle, so the solar altitude would have been about 14 degrees. The sun is just out of shot below the bottom of the frame. All day long there had been a 22 degree halo and most of the time it was a full circle. The 22 degree halo is pretty common; it’s a circular rainbow around the sun caused by hexagonal ice crystals in the upper atmosphere. And indeed there were a lot of high cirrus clouds that day. I see these fairly frequently because I look for them; most people don’t look towards the sun when looking for rainbows.
Continue reading
String concatenation behind the scenes, part one
The first algorithm I ever worked on in the C# compiler was the optimizer that handles string concatenations.(Unfortunately I did not manage to port these optimizations to the Roslyn codebase before I left; hopefully someone will get to that!) It’s all pretty straightforward, but there are a few clever bits that I thought I might discuss today.
What’s the difference? sizeof and Marshal.SizeOf
I often see StackOverflow answers that confuse the sizeof
operator with the Marshal.SizeOf
method. These two operators do different things and can return different results, so it is important to know which is which.
In a nutshell, the difference is: the sizeof
operator takes a type name and tells you how many bytes of managed memory need to be allocated for an instance of that struct.[1. I don’t have to tell long-time readers of this blog that of course this is not necessarily stack memory; structs are allocated off the heap when they are array elements, fields of a class, and so on.] By contrast, Marshal.SizeOf
takes either a type object or an instance of the type, and tells you how many bytes of unmanaged memory need to be allocated. These can be different for a variety of reasons. The name of the type gives you a clue: Marshal.SizeOf
is intended to be used when marshaling a structure to unmanaged memory.
Another difference between the two is that the sizeof
operator can only take the name of an unmanaged type; that is, a struct type whose fields are only integral types, Booleans, pointers and so on. (See the specification for an exact definition.) Marshal.SizeOf
by contrast can take any class or struct type.
Construction destruction
Take a look at this little program skeleton:
class Foo { private int x; private int y; public Foo(int x, int y) { this.x = x; this.y = y; SideEffects.Alpha(); // Notice: does not use "this" } ~Foo() { SideEffects.Charlie(); } } static class SideEffects { public static void Alpha() { ... } public static void Bravo() { ... } public static void Charlie() { ... } public static void M() { Foo foo = new Foo(1, 2); Bravo(); } }
Let’s suppose we have three side effects: Alpha
, Bravo
and Charlie
. What precisely they are is not important. The question is: what do we know about the order in which Alpha
, Bravo
and Charlie
execute when a call to M()
occurs? Continue reading
An integer division identity
Here’s an interesting question that came up on StackOverflow the other day: we know in “real” algebra that the equation (a / b) / c = a / (b * c)
is an “identity”; it is true for any values of a
, b
, and c
.[1. Assuming of course that both sides have a well-defined value.] Does this identity hold for integer arithmetic in C#?