P
Peter Duniho
Jonathan said:For things like this, I generally prefer a table. Of course, my
background is in C++ where tables can be EXTREMELY efficient for things
like this, while C# can be very different.
I'm not sure what you mean by "table". If you're just referring to an
array, they are about as efficient in C# as in C++, and I wouldn't say
they are especially more efficient as compared to other data structures,
as long as those other data structures are in fact used in an efficient way.
Efficiency has a lot to do with context. An array is a pretty
inefficient way to store data if you need random access via something
other than an integer index into the array, and is very efficient if
accessing by integer index is the only expected usage. It just depends.
By the way, modern C++ compilers do a very effective job optimizing
"switch" statements as well. Don't be afraid of them.
![Smile :) :)](/styles/default/custom/smilies/smile.gif)
I like this a lot, and I'll just take your word for it that it would be
much faster than Activator.CreateInstance().
Well, you don't have to take my word for it (and in fact, I encourage
you not to). You can implement it each way, and then benchmark each
implementation.
Of course, even if you find a difference in the benchmark, if your own
runtime usage isn't like the benchmark, the performance difference may
not matter. This is another "it depends". Benchmarks generally are
written to exercise a particular piece of code repeatedly over a large
number of iterations, for long enough that performance differences are
magnified enough to see them.
But if your code only calls this kind of thing once every five minutes,
you'll never ever notice the difference in the real world.
Of course, it's my opinion that there are other reasons than performance
to avoid reflection whenever possible. But if that's your only reason
for avoiding reflection, and you prefer it for some other reason, the
raw performance difference won't tell you the whole story.
While I enjoy static tables where the data is initialized in the EXE
image, C# doesn't really support that anyway.
Not exactly. But note that in C++, except for simple data structures,
they are initialized in very much the same way C# data structures are.
If you declare an array of ints in C++, then sure...the compiler can
generate an actual block of integers to be embedded into the executable
image. But if you've got an array of class instances with a non-trivial
constructor, then C++ still has to go through the same kind of static
initialization C# would do.
And so calling the Add
method to build the collection would not appear to be a disadvantage.
Note that if you really want to, you can embed your dictionary data into
a serialized block of data, and the deserialize it at runtime. The data
can be stored, for example, in your executable's resources.
More overhead, but simpler code.
Another alternative would be to initialize the dictionary from an array
of KeyValuePairs. You'd still need a loop, but at least you'd only have
to write the call to Add() once.
![Smile :) :)](/styles/default/custom/smilies/smile.gif)
Pete