473,666 Members | 2,334 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 2824
"Shawn B." <le****@html.co m> wrote in
news:eT******** ******@tk2msftn gp13.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 ArgumentOutOfRa ngeException(<s nip>);
}

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

why not just...

public void RotateLeft(int count)
{
if (count >= SIZE || count <= 0) {
throw new ArgumentOutOfRa ngeException(<s nip>);
}

Value = (ushort)(((Valu e << 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************ *******@SkPiAlM l.ctiusa.com> wrote in message
news:Xn******** *************** *******@207.46. 248.16...
"Shawn B." <le****@html.co m> wrote in
news:eT******** ******@tk2msftn gp13.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 ArgumentOutOfRa ngeException(<s nip>);
}

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

why not just...

public void RotateLeft(int count)
{
if (count >= SIZE || count <= 0) {
throw new ArgumentOutOfRa ngeException(<s nip>);
}

Value = (ushort)(((Valu e << 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.co m> wrote in message news:%2******** ********@tk2msf tngp13.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************ *******@SkPiAlM l.ctiusa.com> wrote in message
news:Xn******** *************** *******@207.46. 248.16...
"Shawn B." <le****@html.co m> wrote in
news:eT******** ******@tk2msftn gp13.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 ArgumentOutOfRa ngeException(<s nip>);
}

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

why not just...

public void RotateLeft(int count)
{
if (count >= SIZE || count <= 0) {
throw new ArgumentOutOfRa ngeException(<s nip>);
}

Value = (ushort)(((Valu e << 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.jeppese n(#)durius.se> wrote in message
news:ua******** ******@TK2MSFTN GP10.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.co m> wrote in message

news:%2******** ********@tk2msf tngp13.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************ *******@SkPiAlM l.ctiusa.com> wrote in message news:Xn******** *************** *******@207.46. 248.16...
"Shawn B." <le****@html.co m> wrote in
news:eT******** ******@tk2msftn gp13.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 ArgumentOutOfRa ngeException(<s nip>);
}

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

why not just...

public void RotateLeft(int count)
{
if (count >= SIZE || count <= 0) {
throw new ArgumentOutOfRa ngeException(<s nip>);
}

Value = (ushort)(((Valu e << 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.co m> wrote in message
news:eT******** ******@tk2msftn gp13.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
7391
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 interchangeable in all circumstances (as long as they're paired) so there's no overloading to muddy the language. Of course there could be some interesting problems with current code that doesn't make a distinction, but it would be dead easy to fix...
49
2595
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 code sample where newbies expect the two function calls below to both print : def f( list= ): print list.append!(1) f() # prints
50
6342
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 def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the method foo. But the initialization of attr2 is wrong
12
6363
by: Kjetil Kristoffer Solberg | last post by:
What is a mutable struct? regards Kjetil Kristoffer Solberg
8
2439
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
1603
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 some good reasons why it was designed this way. But why not have mutable numbers also in the language. A type which would behave as follows: a = MutableInt(12)
12
3742
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 and hence it is possible to do: txt += "foo bar";
12
3142
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
4205
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 significance of them? Thanks Simon
0
8443
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7385
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, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6192
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.