[Closed] SDK to C# data transfer performance
I have some C# code and an SDK plugin.
Within my C# code, I have an IObject that I get from an INode (I pass an INode containing my SDK SimpleObject to my C# function, and then grab the IObject with [INode].ObjectRef).
I then call the builtin GetWeight function of that IObject a million times like this:
float totalWeight = 0;
for (int q = 0; q < 1000000; q++)
{
totalWeight += obj.GetWeight(q);
}
In my SDK plugin I override the GetWeight function as follows:
override float GetWeight (int i)
{
return i;
}
Even though this code executes relatively quickly, it executes much slower than alternative code that does not involve any SDK calls, like this:
float totalWeight = 0;
for (int q = 0; q < 1000000; q++)
{
totalWeight += q;
}
Obviously I understand that any function calls will always incur a performance penalty, but I don’t think the performance hit I’m experiencing (something around 10x slower with the function call) can be explained by that single call in my loop.
I’m assuming the hit can be explained by the float values needing to be converted from c++ float to C# floats, similar to the big performance hit that happens anytime a MXS value is converted to a C# value.
Would that explain the slowdown? And is there any way of improving performance?
obj.GetWeight(q);
i think this is the problem. when code goes in c++ area and returns back in c#. i just guess that it needs some memory manipulations…
so i would make a c++ method (GetWeights) that takes a OUT parameter – float* or FloatTab, and fills this float buffer with specified number of calls or using a passed list of indexes. It will make all weight calculations inside c++ area*
void Object::GetWeights(IntTab ids, FloatTab& weights)
{
weights.SetCount(ids.Count());
for (int k=0; k < ids.Count(); k++) weights[k] = GetWeight(ids[k]);
}
it might also calculate the sum of weights, or average…
Thanks denisT,
Unfortunately the loops have to happen completely in C# so I can’t cheat like that. But luckily I discovered that PInvoke + [SuppressUnmanagedCodeSecurity] gives a decent performance boost.