473,387 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

c# - fixed statement performance / penalty

This is probably beaten to death subject...

Does anyone have a good idea of what penalties are for using Fixed
statement in c#. On one side it allows for much greater flexibility
with casts and array manipulations (i'm leaving access to legacy code
out of the scope of this message) on the other hand fixed statement
does consume resources to execute, not to mention if "everything" is
fixed, then dynamic object reallocation becomes impossible, thus
removing a good portion of .net internal memory management
optimizations. Did anyone have any gains / degradations in large scale
implementation with "fixed" vs. "proper" implementations?

Thx.
Nov 15 '05 #1
5 8306
Maybe a more important question is why would you want to use this statement
and when is it justified necessary to use it?
I had never heard of the statement before :|

Greetz,
-- Rob.

Arthur Mnev wrote:
This is probably beaten to death subject...

Does anyone have a good idea of what penalties are for using Fixed
statement in c#. On one side it allows for much greater flexibility
with casts and array manipulations (i'm leaving access to legacy code
out of the scope of this message) on the other hand fixed statement
does consume resources to execute, not to mention if "everything" is
fixed, then dynamic object reallocation becomes impossible, thus
removing a good portion of .net internal memory management
optimizations. Did anyone have any gains / degradations in large scale
implementation with "fixed" vs. "proper" implementations?

Thx.

Nov 15 '05 #2
I'd think of fixed C# objects as of C/C++ object allocated in heap...

"Arthur Mnev" <ar********@yahoo.com> wrote in message
news:63**************************@posting.google.c om...
This is probably beaten to death subject...

Does anyone have a good idea of what penalties are for using Fixed
statement in c#. On one side it allows for much greater flexibility
with casts and array manipulations (i'm leaving access to legacy code
out of the scope of this message) on the other hand fixed statement
does consume resources to execute, not to mention if "everything" is
fixed, then dynamic object reallocation becomes impossible, thus
removing a good portion of .net internal memory management
optimizations. Did anyone have any gains / degradations in large scale
implementation with "fixed" vs. "proper" implementations?

Thx.

Nov 15 '05 #3
Rob,

Fixed statement is used in unsafe code to obtain a pointer to a memory
location that otherwise is volatile. .NET runtime can reallocate any
object to a more suitable location at any time, therefore, if you want
to work with memory directly (or as directly as .NET allows you) you
need to fix an object to prevent runtime from moving it elsewhere.
Where would it be used ... lets see a very typical example - read an
array of structures from a file, cast an array of bytes into array of
integers etc...

Example, - assume that you need to perform an operation with an
integer but have an array of bytes.

in a traditional scenario, you would need to loop through byte array,
using BitConverter class convert a set of 4 bytes into an integer.
That is costly as every time you would loop you would have to make a
copy of the value; instead:

byte[] mybytearr = new byte[100];

public static Main()
{
IntWorks(mybytearr);
}

public unsafe void IntWorks(byte[] byteArray)
{
Int32 f_integer_to_work_on = 0;
fixed (byte* byteArrayPtr = byteArray)
{
for (op_counter = 0; op_counter < (byteArray / sizeof(Int32));
op_counter++)
{
f_integer_to_work_on = (Int32*) byteArrayPtr[op_counter];
// do some work here with that integer
// done
}
}
}

as you can see there is no data copying involved as you obtain a
reference to a memory location and treat it as if it was integer.
Naturally you need to make sure that you dont try to access location
past the real array, but then again, it is just following the rules of
programming.

hope it helps.

"Rob Tillie" <Ro********@student.tul.edu> wrote in message news:<#t**************@TK2MSFTNGP10.phx.gbl>...
Maybe a more important question is why would you want to use this statement
and when is it justified necessary to use it?
I had never heard of the statement before :|

Greetz,
-- Rob.

Arthur Mnev wrote:
This is probably beaten to death subject...

Does anyone have a good idea of what penalties are for using Fixed
statement in c#. On one side it allows for much greater flexibility
with casts and array manipulations (i'm leaving access to legacy code
out of the scope of this message) on the other hand fixed statement
does consume resources to execute, not to mention if "everything" is
fixed, then dynamic object reallocation becomes impossible, thus
removing a good portion of .net internal memory management
optimizations. Did anyone have any gains / degradations in large scale
implementation with "fixed" vs. "proper" implementations?

Thx.

Nov 15 '05 #4
Minor correction
Line: f_integer_to_work_on = (Int32*) byteArrayPtr[op_counter];
Should read: f_integer_to_work_on = ((Int32*) byteArrayPtr)[op_counter];
Nov 15 '05 #5
Still doesnt answer my original question :) any idea on how much
overhead fixing an object is?

"Val Savvateev" <vs********@meridium.com_NO_SPAM> wrote in message news:<#1**************@TK2MSFTNGP11.phx.gbl>...
I'd think of fixed C# objects as of C/C++ object allocated in heap...

"Arthur Mnev" <ar********@yahoo.com> wrote in message
news:63**************************@posting.google.c om...
This is probably beaten to death subject...

Does anyone have a good idea of what penalties are for using Fixed
statement in c#. On one side it allows for much greater flexibility
with casts and array manipulations (i'm leaving access to legacy code
out of the scope of this message) on the other hand fixed statement
does consume resources to execute, not to mention if "everything" is
fixed, then dynamic object reallocation becomes impossible, thus
removing a good portion of .net internal memory management
optimizations. Did anyone have any gains / degradations in large scale
implementation with "fixed" vs. "proper" implementations?

Thx.

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Kenneth McDonald | last post by:
Now that I'm back to Python and all the new (to me) cool features, I find I'm using properties a lot, i.e. I'm defining: foo = property(fset=..., fget=...) for a number of properties, in many...
7
by: Michael Andersson | last post by:
Hi! Does the use of exception handling induce a performance penalty during the execution of non exception handling code? Regards, /Michael
12
by: Fred | last post by:
Has anyone a link or any information comparing c and c++ as far as execution speed is concerned? Signal Processing algorithms would be welcome... Thanks Fred
1
by: Peter Bär | last post by:
A Question to the C#/.Net Gods of this forum: are there performance penalties when i compile (C#, FW1.1, ASP.NET, Studio2003) a central baseclass in a different assembly than all the derived...
2
by: Peter Bär | last post by:
A Question to the C#/.Net Gods of this forum: are there performance penalties when i compile (C#, FW1.1, ASP.NET, Studio2003) a central baseclass in a different assembly than all the derived...
2
by: Marty | last post by:
Hi, I would like to know about DLL and performance gain/penalty in an application. Let's say that I have a very big application and for component portability and easy maintenance, we fragmented...
5
by: toton | last post by:
Hi, I want a few of my class to overload from a base class, where the base class contains common functionality. This is to avoid repetition of code, and may be reducing amount of code in binary,...
9
by: =?Utf-8?B?TWlrZQ==?= | last post by:
Hi. I have a class with an collection property. The collection property is sometimes equal to nothing. The class has a function named 'Exists' which returns TRUE if the specified string exists...
10
by: JohnO | last post by:
Hi All, This question is related to iSeries V5R4 and db2. I want to implement an AFTER DELETE trigger to save the deleted rows to an archive table, I initially defined it as a FOR EACH...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.