How expensive are points in .NET?
In the past, in the C and C ++ lands, splitting of decomposed pointers was considered by some to be relatively costly operations if performed in a closed loop.
You don't want to be caught:
for (int i = 0; i < 10000000; i++)
{
j->k->l->m->n->o->p->dosomeworknowthatwereherewhynoteh();
}
because you can waste precious milliseconds. (Yes, I'm a little sarcastic!)
Moving to the .NET world ...
It is more expensive
System.Runtime.InteropServices.Marshal.WriteInt32(Abort, 1)
what is it?
Imports System.Runtime.InteropServices.Marshal
.
.
.
WriteInt32(Abort, 1)
This is a comparison between apples and oranges.
System.Runtime.InteropServices.Marshal.WriteInt32(Abort, 1)
equivalent to this in C ++:
Foo::Bar::Baz::Func(a, b);
In other words, namespaces are nullified by the compiler.
To get something equivalent, you might have something like this:
public class Foo {
public Person Agent { get; }
}
Foo f = getFooFromWhereEver();
f.Agent.Name.ToString().ToLower();
In this case, imagine Person has a Name property, which is a string. In this case, the dot chain makes four method calls, at least one of which is virtual, but most likely not all of them are invariant, so calling them multiple times is redundant. I say "rather ..." because it depends on the implementation of the agent and the person.
source to share
Dots in namespaces are not expensive; they are solved by the compiler at compile time, not at run time. (In fact, the opposite is true, be picky: if you use / import statements to shorten your code, type references will be expanded to the fully qualified type name, including the namespace, at compile time.) However, the points to reach properties or methods have a certain cost.
These two should have the same performance:
System.Collections.Generic.List<string> myList = new System.Collections.Generic.List<string>();
// using System.Collections.Generic
List<string> myList = new List<string>();
When accessing a property at the same time, the properties can cost:
for (int i = 0; i < 100000; i++)
{
int n = this.ActiveControl.Size.Width;
}
// this should be faster
int width = this.ActiveControl.Size.Width;
for (int i = 0; i < 100000; i++)
{
int n = width;
}
source to share
there is no difference in your import example. The import statement just makes sure you don't have to enter the full path every time.
however, if you wrote:
for(i=0; i<10000; i++)
{
classInstance.memberclass.memberclass.memberclass.memberclass.writeInt32(bla);
}
then yes it would probably be better to write:
SomeClass someclass = classInstance.memberclass.memberclass.memberclass.memberclass;
for(i=0; i<10000; i++)
{
someClass.writeInt32(bla);
}
source to share
Actually, in both of your two cases, I expect the results to be equal.
In example C, you actually look at an object at runtime and play it out, so it's no surprise that people think of it as an expensive operation. But in the C # example, the "points" you talk about are statically static at compile time.
source to share
I wrote detailed articles on my blog and I also have microsecond based calculations, but yes, when you add everything up, you can of course differentiate.
http://akashkava.com/blog/?p=95
However my article does various calculations, it also calculates namedvaluecollection and resource access times, etc. But it can be helpful to understand how things work.
source to share