Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In .net , a Long is always 8 bytes length, no matter if you are compiling it for 32 or 64 bits, this is causing me some issues with w32 API structures which expect a 8 byte length integers for 64bits and a 4 byte length for 32 bits.
(in C++ we would just use SIZE_T, for example)

So far, i started using IntPtr data type for those cases, as its length does change from 4 to 8 based on the platform, and then just use IntPtr.toInt64 to read its value as a Long.

But since my API calls are time/memory critical I'm not 100% sure that this is the best approach, so far i have noticed that, upon read of 200 values and measure the time its adding an overhead of 2-3ms using IntPtr compared to Integer/Long values.

Another option would be define 2 different structures, one as integer to be used in 32bit and other as long to be used on 64bit, but this doesn't not seems very elegant to me.

Is there any other option available to deal with this? Any Marshal data type or something that im not aware of?

And as a plus, is there any benefit as declaring Marshal.DataTypes on the structure if it not needed? (like for integers/longs) ? does it speedup anything?

Thanks for the comments and sorry for my bad English, hope i make myself clear.
Posted
Comments
Sergey Alexandrovich Kryukov 25-Jan-12 23:59pm    
To me, it depends on where did you get the value of platform-dependent type and what you do with this value. Can it be possible that managed code just transparently move this value (stores, copies, etc), but performs any integer arithmetic on it? In this way, conversion to .NET fixed-size integer type would not be needed. All you time overhead is only due the conversion like ToInt64. Could you list all places where such values are used?
--SA
creizlein 26-Jan-12 0:11am    
Thank you very much for your reply, i do need to perform arithmetic operations on the read values for sure, as i need to evaluate them before actually use/display those.

To make an example, the values I'm reading come from an call to psapi.dll:GetPerformanceInfo(), and it returns a structure, i use this to get some system counters, but I'm not 100% what you mean by "list all places...".
I read those values and print them on WinForm App, i need those to be integers values as i have to apply some math to them before actually get displayed.

Please let me know if that is the kind of information you need.

As i understand, using ToInt64 would imply a conversion, and that may be the overhead for sure, but then what i would need to use to avoid such conversion if Long datatypes always use 8-byte?

Regards,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900