Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 556391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:54:52+00:00 2026-05-13T11:54:52+00:00

I’m currently optimizing an application, one of the operations that is done very often

  • 0

I’m currently optimizing an application, one of the operations that is done very often is reading and writing binary. I need 2 types of functions:

Set(byte[] target, int index, int value);

int Get(byte[] source, int index);

These functions are needed for signed and unsigned short, int and long in big and little endian order.

Here are some examples i’ve made, but i need a evaluation about the advantages and disadvantages:

first method is using Marshal to write the value into the memory of the byte[], the second is using plain pointers to accomplish this and the third uses BitConverter and BlockCopy to do this

unsafe void Set(byte[] target, int index, int value)
{
    fixed (byte* p = &target[0])
    {
        Marshal.WriteInt32(new IntPtr(p), index, value);
    }
}

unsafe void Set(byte[] target, int index, int value)
{
    int* p = &value;
    for (int i = 0; i < 4; i++)
    {
        target[offset + i] = *((byte*)p + i);
    }
}

void Set(byte[] target, int index, int value)
{
    byte[] data = BitConverter.GetBytes(value);
    Buffer.BlockCopy(data, 0, target, index, data.Length);
}

And here are the Read/Get methods:

the first is using Marshal to read the value from the byte[], the second is using plain pointers and the third is using BitConverter again:

unsafe int Get(byte[] source, int index)
{
    fixed (byte* p = &source[0])
    {
        return Marshal.ReadInt32(new IntPtr(p), index);
    }
}

unsafe int Get(byte[] source, int index)
{
    fixed (byte* p = &source[0])
    {
        return *(int*)(p + index);
    }
}

unsafe int Get(byte[] source, int index)
{
    return BitConverter.ToInt32(source, index);
}

boundary checking needs to be done but isn’t part of my question yet…

I would be pleased if someone can tell what would be the best and fastest way in this case or give me some other solutions to work on. A generic solution would be preferable


I Just did some performance testing, here are the results:

Set Marshal: 45 ms, Set Pointer: 48 ms, Set BitConverter: 71 ms
Get Marshal: 45 ms, Get Pointer: 26 ms, Get BitConverter: 30 ms

it seems that using pointers is the fast way, but i think Marshal and BitConverter do some internal checking… can someone verify this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:54:53+00:00Added an answer on May 13, 2026 at 11:54 am

    Important: if you only need the one endian, see the pointer magic by wj32 / dtb


    Personally, I would be writing directly to a Stream (perhaps with some buffering), and re-using a shared buffer that I can generally assume is clean. Then you can make some shortcuts and assume index 0/1/2/3.

    Certainly don’t use BitConverter, as that can’t be used for both little/big-endian, which you require. I would also be inclined to just use bit-shifting rather than unsafe etc. It is actally the fastest, based on the following (so I’m glad that this is how I already do it my code here, look for EncodeInt32Fixed):

    Set1: 371ms
    Set2: 171ms
    Set3: 993ms
    Set4: 91ms <==== bit-shifting ;-p
    

    code:

    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    static class Program
    {
        static void Main()
        {
            const int LOOP = 10000000, INDEX = 100, VALUE = 512;
            byte[] buffer = new byte[1024];
            Stopwatch watch;
    
            watch = Stopwatch.StartNew();
            for (int i = 0; i < LOOP; i++)
            {
                Set1(buffer, INDEX, VALUE);
            }
            watch.Stop();
            Console.WriteLine("Set1: " + watch.ElapsedMilliseconds + "ms");
    
            watch = Stopwatch.StartNew();
            for (int i = 0; i < LOOP; i++)
            {
                Set2(buffer, INDEX, VALUE);
            }
            watch.Stop();
            Console.WriteLine("Set2: " + watch.ElapsedMilliseconds + "ms");
    
            watch = Stopwatch.StartNew();
            for (int i = 0; i < LOOP; i++)
            {
                Set3(buffer, INDEX, VALUE);
            }
            watch.Stop();
            Console.WriteLine("Set3: " + watch.ElapsedMilliseconds + "ms");
    
            watch = Stopwatch.StartNew();
            for (int i = 0; i < LOOP; i++)
            {
                Set4(buffer, INDEX, VALUE);
            }
            watch.Stop();
            Console.WriteLine("Set4: " + watch.ElapsedMilliseconds + "ms");
    
            Console.WriteLine("done");
            Console.ReadLine();
        }
        unsafe static void Set1(byte[] target, int index, int value)
        {
            fixed (byte* p = &target[0])
            {
                Marshal.WriteInt32(new IntPtr(p), index, value);
            }
        }
    
        unsafe static void Set2(byte[] target, int index, int value)
        {
            int* p = &value;
            for (int i = 0; i < 4; i++)
            {
                target[index + i] = *((byte*)p + i);
            }
        }
    
        static void Set3(byte[] target, int index, int value)
        {
            byte[] data = BitConverter.GetBytes(value);
            Buffer.BlockCopy(data, 0, target, index, data.Length);
        }
        static void Set4(byte[] target, int index, int value)
        {
            target[index++] = (byte)value;
            target[index++] = (byte)(value >> 8);
            target[index++] = (byte)(value >> 16);
            target[index] = (byte)(value >> 24);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.