Hint: don't try to do useless optimizations like this, unless you're 100% sure it's actually a bottleneck in your program, which I highly doubt is the case here.
Hint: don't try to do useless optimizations like this, unless you're 100% sure it's actually a bottleneck in your program, which I highly doubt is the case here.
Not sure that adding the overhead of the native call and marshalling is really faster for 16 bytes...
Btw. More optimized memcpy than byte per byte in C#.
Code:
/// <summary>
/// Copies the values of num bytes from the location pointed by source directly to the memory block
/// pointed by destination.
///
/// The underlying type of the objects pointed by both the source and destination pointers are irrelevant
/// for this function; The result is a binary copy of the data.
/// </summary>
/// <param name="dest">Pointer to the destination array where the content is to be copied.</param>
/// <param name="src">Pointer to the source of data to be copied.</param>
/// <param name="num">Number of bytes to copy.</param>
/// <returns>The destination is returned.</returns>
public static void* memcpy(void* dest, void* src, size_t num)
{
//The use of Int32 on both x86 and x64 is the best solution to get the best speed.
//Probably due to the aligment of the data.
Int32 amount = num / sizeof(size_t);
for (Int32 i = 0; i < amount; ++i)
((size_t*)dest)[i] = ((size_t*)src)[i];
amount = num % sizeof(size_t);
Int32 pos = num - amount;
for (Int32 i = 0; i < amount; ++i)
(((Byte*)dest) + pos)[i] = (((Byte*)src) + pos)[i];
return dest;
}
Btw. More optimized memcpy than byte per byte in C#.
Code:
/// <summary>
/// Copies the values of num bytes from the location pointed by source directly to the memory block
/// pointed by destination.
///
/// The underlying type of the objects pointed by both the source and destination pointers are irrelevant
/// for this function; The result is a binary copy of the data.
/// </summary>
/// <param name="dest">Pointer to the destination array where the content is to be copied.</param>
/// <param name="src">Pointer to the source of data to be copied.</param>
/// <param name="num">Number of bytes to copy.</param>
/// <returns>The destination is returned.</returns>
public static void* memcpy(void* dest, void* src, size_t num)
{
//The use of Int32 on both x86 and x64 is the best solution to get the best speed.
//Probably due to the aligment of the data.
Int32 amount = num / sizeof(size_t);
for (Int32 i = 0; i < amount; ++i)
((size_t*)dest)[i] = ((size_t*)src)[i];
amount = num % sizeof(size_t);
Int32 pos = num - amount;
for (Int32 i = 0; i < amount; ++i)
(((Byte*)dest) + pos)[i] = (((Byte*)src) + pos)[i];
return dest;
}
/// <summary>
/// size_t corresponds to the integer data type returned by the language operator sizeof and is defined
/// in the <cstdlib> header file (among others) as an unsigned integer type.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct size_t
{
public Int32 value;
public size_t(Int32 val) { value = val; }
public static implicit operator Int32(size_t size)
{ return size.value; }
}
#endregion
size_t is not actually a 32 bit integer. It depends on the computer architecture.
Also size_t has to be unsigned.
On x64 size_t would be an unsigned 64 bit integer.
However you can't do that correctly in C#, but it should at least be uint and not int.
-- same reference tho.
You could use UIntPtr (which is an unsigned 32 bit integer on x86 and unsigned 64 bit integer on x64), or you could use your own conditional compilation symbols like:
Code:
struct size_t
{
#if x86
public UInt32 Value;
#elif x64
public UInt64 Value;
#endif
}
But yeah, I guess it should be an unsigned integer just like the documentation of the struct says: "and is defined in the <cstdlib> header file (among others) as an unsigned integer type.".
But for some stupid reason, the sizeof operator actually returns a signed integer in C#.
size_t is not actually a 32 bit integer. It depends on the computer architecture.
Also size_t has to be unsigned.
On x64 size_t would be an unsigned 64 bit integer.
However you can't do that correctly in C#, but it should at least be uint and not int.
-- same reference tho.
Yes, I know. But you all got the idea... And it was based on the fact that sizeof will return an int in C#, idem for .Length(). Plus, for the for-loop itself, the data is aligned on a 4-bytes boundary in both architectures.
[B]Webdesign, Coding und SEO (Search Engine optimizing) 05/13/2013 - Coders Trading - 0 Replies Hallo Epvp Forum,
ich Biete euch meine Skills gegen Bezahlung an.
Was ich euch Bieten kann:
Webdesign (HTML + CSS + Photoshop)
Coding (einfache PHP scripts)
SEO (Professionelles SEO mit keyword suche, website optimierung und bessere Indexierung in Suchmaschienen und backlinks)
Optimizing to use less CPU? 10/20/2011 - CO2 Private Server - 26 Replies Hello everybody.
I would like to ask you how could I optimize a server to use less CPU and use more RAM instead?
I know that some things can't be taken by the RAM but even so there can be a lot of things to make a server run faster.
So how could that be made?
Increasing FPS and Optimizing PC [To get rid of SF lag or any games] 03/25/2010 - Soldier Front - 7 Replies Step 1:
Cleaning and Defragmenting Hard Disk
A) Download Ccleaner
Go to FileHippo.com - Download Free Software
Next click Ccleaner and click "Download Latest Version"