473,508 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Custom Mutable/Immutable ValueType

Greetings,

I have created a series of objects for dealing with values at the binary
level (http//:www.visualassembler.com/binary). Each time I do anything with
them it seems I have to return a new instance of the object (look in the
operator overloads). This means it is immutable. Is there a way I can make
it a bit more, mutable? For performance reasons, of course. I've about
squeezed every ounce of performance I could and now need to focus on this
aspect.
Thanks,
Shawn
Nov 15 '05 #1
5 2816
"Shawn B." <le****@html.com> wrote in
news:eT**************@tk2msftngp13.phx.gbl:
I have created a series of objects for dealing with values at the
binary level (http//:www.visualassembler.com/binary). Each time I do
anything with them it seems I have to return a new instance of the
object (look in the operator overloads). This means it is immutable.
Is there a way I can make it a bit more, mutable? For performance
reasons, of course. I've about squeezed every ounce of performance I
could and now need to focus on this aspect.


Maybe its a bit obvious, but why do you have to return a value at all??
For example, you have:

public ushort RotateLeft(int count)
{
if (count >= SIZE || count <= 0) {
throw new ArgumentOutOfRangeException(<snip>);
}

return (ushort)(((Value << count) | (Value >> (SIZE-count))) &
0xFFFF);
}

why not just...

public void RotateLeft(int count)
{
if (count >= SIZE || count <= 0) {
throw new ArgumentOutOfRangeException(<snip>);
}

Value = (ushort)(((Value << count) | (Value >> (SIZE-count))) &
0xFFFF);
}

-mbray
Nov 15 '05 #2
That's not the one I'm talking about. It's not an overloaded operator,
where I'm forced to return a new instance of the object. Besides, I don't
want to have to say (I already tried it this way) bin.RotateLeft(2) I want
to say (to keep it consistant with the rest of the behavior: int x =
bin.RotateLeft(2); or something.

Besides, in benchmarking 1 million iteration of each particular operation, I
find that the RotateLeft and RotateRight are twice as fast as when I use any
of the operators (35 ms for 1 million RotateLeft/Right, 70MS for 1 million
bin++, bin--, bin >>= 1, etc.).
Thanks,
Shawn
"Michael Bray" <mb*******************@SkPiAlMl.ctiusa.com> wrote in message
news:Xn******************************@207.46.248.1 6...
"Shawn B." <le****@html.com> wrote in
news:eT**************@tk2msftngp13.phx.gbl:
I have created a series of objects for dealing with values at the
binary level (http//:www.visualassembler.com/binary). Each time I do
anything with them it seems I have to return a new instance of the
object (look in the operator overloads). This means it is immutable.
Is there a way I can make it a bit more, mutable? For performance
reasons, of course. I've about squeezed every ounce of performance I
could and now need to focus on this aspect.


Maybe its a bit obvious, but why do you have to return a value at all??
For example, you have:

public ushort RotateLeft(int count)
{
if (count >= SIZE || count <= 0) {
throw new ArgumentOutOfRangeException(<snip>);
}

return (ushort)(((Value << count) | (Value >> (SIZE-count))) &
0xFFFF);
}

why not just...

public void RotateLeft(int count)
{
if (count >= SIZE || count <= 0) {
throw new ArgumentOutOfRangeException(<snip>);
}

Value = (ushort)(((Value << count) | (Value >> (SIZE-count))) &
0xFFFF);
}

-mbray

Nov 15 '05 #3
Hi Shawn,

I have no suggestion here. You have two choices, either keep it as is, or ditch the operators.
If you lose the operators, you'll have to have loads of method calls that changes the instance instead.
You'll lose consistency with the framework. If you suspect that oher people will be using these classes
then you'll want to keep the operators for clarity, so people will know how to use the classes.
On the other hand, you could use both. In other words, keep the operators in there and adding all the instance modifier methods,
giving users of the choice of coding in an optimized way or in a readable/manageable way.

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
That's not the one I'm talking about. It's not an overloaded operator,
where I'm forced to return a new instance of the object. Besides, I don't
want to have to say (I already tried it this way) bin.RotateLeft(2) I want
to say (to keep it consistant with the rest of the behavior: int x =
bin.RotateLeft(2); or something.

Besides, in benchmarking 1 million iteration of each particular operation, I
find that the RotateLeft and RotateRight are twice as fast as when I use any
of the operators (35 ms for 1 million RotateLeft/Right, 70MS for 1 million
bin++, bin--, bin >>= 1, etc.).
Thanks,
Shawn
"Michael Bray" <mb*******************@SkPiAlMl.ctiusa.com> wrote in message
news:Xn******************************@207.46.248.1 6...
"Shawn B." <le****@html.com> wrote in
news:eT**************@tk2msftngp13.phx.gbl:
I have created a series of objects for dealing with values at the
binary level (http//:www.visualassembler.com/binary). Each time I do
anything with them it seems I have to return a new instance of the
object (look in the operator overloads). This means it is immutable.
Is there a way I can make it a bit more, mutable? For performance
reasons, of course. I've about squeezed every ounce of performance I
could and now need to focus on this aspect.


Maybe its a bit obvious, but why do you have to return a value at all??
For example, you have:

public ushort RotateLeft(int count)
{
if (count >= SIZE || count <= 0) {
throw new ArgumentOutOfRangeException(<snip>);
}

return (ushort)(((Value << count) | (Value >> (SIZE-count))) &
0xFFFF);
}

why not just...

public void RotateLeft(int count)
{
if (count >= SIZE || count <= 0) {
throw new ArgumentOutOfRangeException(<snip>);
}

Value = (ushort)(((Value << count) | (Value >> (SIZE-count))) &
0xFFFF);
}

-mbray


Nov 15 '05 #4
I had thought about this approach (I was doing so with the Shift operators)
but that extra jump cringes me. I shouldn't complain about 70 ms for 1
million X++'s, however, the 64-bit version of the class is 2x slower than
the others, the others are the same relative performance in every case.

I think I'll keep it as it is. I can emulate sufficiently a 500Mhz 6502 at
this rate, nothing to complain about.
Thanks,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:ua**************@TK2MSFTNGP10.phx.gbl...
Hi Shawn,

I have no suggestion here. You have two choices, either keep it as is, or ditch the operators. If you lose the operators, you'll have to have loads of method calls that changes the instance instead. You'll lose consistency with the framework. If you suspect that oher people will be using these classes then you'll want to keep the operators for clarity, so people will know how to use the classes. On the other hand, you could use both. In other words, keep the operators in there and adding all the instance modifier methods, giving users of the choice of coding in an optimized way or in a readable/manageable way.
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2****************@tk2msftngp13.phx.gbl...
That's not the one I'm talking about. It's not an overloaded operator,
where I'm forced to return a new instance of the object. Besides, I don't want to have to say (I already tried it this way) bin.RotateLeft(2) I want to say (to keep it consistant with the rest of the behavior: int x =
bin.RotateLeft(2); or something.

Besides, in benchmarking 1 million iteration of each particular operation, I find that the RotateLeft and RotateRight are twice as fast as when I use any of the operators (35 ms for 1 million RotateLeft/Right, 70MS for 1 million bin++, bin--, bin >>= 1, etc.).
Thanks,
Shawn
"Michael Bray" <mb*******************@SkPiAlMl.ctiusa.com> wrote in message news:Xn******************************@207.46.248.1 6...
"Shawn B." <le****@html.com> wrote in
news:eT**************@tk2msftngp13.phx.gbl:

> I have created a series of objects for dealing with values at the
> binary level (http//:www.visualassembler.com/binary). Each time I do > anything with them it seems I have to return a new instance of the
> object (look in the operator overloads). This means it is immutable. > Is there a way I can make it a bit more, mutable? For performance
> reasons, of course. I've about squeezed every ounce of performance I > could and now need to focus on this aspect.

Maybe its a bit obvious, but why do you have to return a value at all?? For example, you have:

public ushort RotateLeft(int count)
{
if (count >= SIZE || count <= 0) {
throw new ArgumentOutOfRangeException(<snip>);
}

return (ushort)(((Value << count) | (Value >> (SIZE-count))) &
0xFFFF);
}

why not just...

public void RotateLeft(int count)
{
if (count >= SIZE || count <= 0) {
throw new ArgumentOutOfRangeException(<snip>);
}

Value = (ushort)(((Value << count) | (Value >> (SIZE-count))) &
0xFFFF);
}

-mbray



Nov 15 '05 #5
After further looking into the StringBuilder class, it appears mutable
because it is an array of chars treated in a similar manner to a linked
list. That would explain it. Not possible for me in these classes.
Thanks,
Shawn

"Shawn B." <le****@html.com> wrote in message
news:eT**************@tk2msftngp13.phx.gbl...
Greetings,

I have created a series of objects for dealing with values at the binary
level (http//:www.visualassembler.com/binary). Each time I do anything with them it seems I have to return a new instance of the object (look in the
operator overloads). This means it is immutable. Is there a way I can make it a bit more, mutable? For performance reasons, of course. I've about
squeezed every ounce of performance I could and now need to focus on this
aspect.
Thanks,
Shawn

Nov 15 '05 #6

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

Similar topics

17
7377
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
49
2555
by: Mark Hahn | last post by:
As we are addressing the "warts" in Python to be fixed in Prothon, we have come upon the mutable default parameter problem. For those unfamiliar with the problem, it can be seen in this Prothon...
50
6290
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
12
6343
by: Kjetil Kristoffer Solberg | last post by:
What is a mutable struct? regards Kjetil Kristoffer Solberg
8
2429
by: Mateusz Łoskot | last post by:
Hi, I know iterator categories as presented by many authors: Stroustrup, Josuttis and Koenig&Moo: Input <---| |<--- Forward <--- Bidirectional <--- Random Output <---|
13
1590
by: Suresh Jeevanandam | last post by:
# I am new to python. In python all numbers are immutable. This means there is one object ( a region in the memory ) created every time we do an numeric operation. I hope there should have been...
12
3717
by: Water Cooler v2 | last post by:
Are JavaScript strings mutable? How're they implemented - 1. char arrays 2. linked lists of char arrays 3. another data structure I see that the + operator is overloaded for the string class...
12
3126
by: Vincent RICHOMME | last post by:
Hi, I am currently implementing some basic classes from .NET into modern C++. And I would like to know if someone would know a non mutable string class.
2
4195
by: Simon Woods | last post by:
Hi I wonder if someone could explain the above to me. The examples I've seen seem to indicate that it is effectively a read-only class, but I'm sure that's being over simplistic. But what is the...
0
7225
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
7385
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...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3195
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.