473,804 Members | 3,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Operator Performance

Still been beating on my Multi Unit Scalar structure and...it's coming
along well. Since it is a core component of my app that virtually
everything builds on, even the smalles amount of performance gain is
worth it.

But I think I have hit a wall.

One million operations now average around 98ms give or take. If it's
Power 0/1 (linear unit) or Power 2+ (area, volume, etc) unit makes no
measurable difference. The additional loop makes less difference in
performance than planetary alignment. (Code is below sig for those who
wanna check it out).

So just out of boredom and because I have nothing better to do, I went
and measured the raw time the operator takes.

Namely:

public static Scalar operator + (Scalar a, Scalar b)
{
return new Scalar();
}

Result: average around 40ms!!

Wait a sec...I am spending almost half my time just in overhead for
the operator? I am betting another good portion goes to the call to
the conversion function. Making my bottom line that the operator
overhead itself is taking magnitudes more time than the actual
operation being performed.

So my question comes here: Is there anything I can do to reduce the
raw overhead of the operator itself? Compiler flag? option? attribute?
virgin sacrifice to bill gates?

Thoughts and suggestions would be very welcome!!

Thanks,

--
Stephan
2003 Yamaha R6

kimi no koto omoidasu hi
nante nai no wa
kimi no koto wasureta toki ga nai kara
public static Scalar operator + (Scalar a, Scalar b)
{
if (a.Power != b.Power) throw new
System.InvalidO perationExcepti on("Operator+po w");

switch(a.Unit)
{
case ScalarUnit.Cent imeter:
return new Scalar(a.Value +
b.GetCentimeter Value(), ScalarUnit.Cent imeter, a.Power);
case ScalarUnit.Inch :
return new Scalar(a.Value + b.GetInchValue( ),
ScalarUnit.Inch , a.Power);
case ScalarUnit.Mil:
return new Scalar(a.Value + b.GetMilValue() ,
ScalarUnit.Mil, a.Power);
case ScalarUnit.Mill imeter:
return new Scalar(a.Value +
b.GetMillimeter Value(), ScalarUnit.Mill imeter, a.Power);
case ScalarUnit.Degr ee:
return new Scalar(a.Value +
b.GetDegreeValu e(), ScalarUnit.Degr ee, a.Power);
case ScalarUnit.Radi an:
return new Scalar(a.Value +
b.GetRadianValu e(), ScalarUnit.Radi an, a.Power);
default:
throw new
System.InvalidO perationExcepti on("Operator+") ;
}
}
public double GetMillimeterVa lue()
{
double scale;
switch (this.Unit)
{
case ScalarUnit.Cent imeter:
scale = 10.0;
break;
case ScalarUnit.Inch :
scale = 25.4;
break;
case ScalarUnit.Mil:
scale = 0.0254;
break;
case ScalarUnit.Mill imeter:
return Value;
default:
throw new
System.InvalidO perationExcepti on("Cannot convert unit.");
}

if (Power 0)
{
int p = Power - 1;

while (p-- != 0)
{
scale *= scale;
}

return Value * scale;
}

return Value * scale;
}
Jul 3 '06 #1
2 1793
Stephan Rose <ke****@somrek. diespammer.netw rote:
Still been beating on my Multi Unit Scalar structure and...it's coming
along well. Since it is a core component of my app that virtually
everything builds on, even the smalles amount of performance gain is
worth it.

But I think I have hit a wall.
[...]
So my question comes here: Is there anything I can do to reduce the
raw overhead of the operator itself? Compiler flag? option? attribute?
virgin sacrifice to bill gates?
The operator is translated directly into a static method call. So, your
problem would appear to be that static method call with struct
parameters is too expensive. There are two alternatives:

1) Use a reference class instead of a struct. (This is only slightly
faster due to overhead of GC.)

2) Use a static method which accepts ref and out parameters rather than
relying on copying. E.g.:

public static void Add(out Scalar result, ref Scalar left,
ref Scalar right)

Both of these are faster to call than a static method relying on copies,
where the body simply returns an empty Scalar, for a Scalar containing 1
double, 1 integer and 1 enumeration value.

Don't forget that the more complex your method becomes, the less likely
the JIT compiler is to inline it.

-- Barry

--
http://barrkel.blogspot.com/
Jul 4 '06 #2
On Tue, 04 Jul 2006 04:12:18 +0100, Barry Kelly
<ba***********@ gmail.comwrote:
>Stephan Rose <ke****@somrek. diespammer.netw rote:
>Still been beating on my Multi Unit Scalar structure and...it's coming
along well. Since it is a core component of my app that virtually
everything builds on, even the smalles amount of performance gain is
worth it.

But I think I have hit a wall.
[...]
>So my question comes here: Is there anything I can do to reduce the
raw overhead of the operator itself? Compiler flag? option? attribute?
virgin sacrifice to bill gates?

The operator is translated directly into a static method call. So, your
problem would appear to be that static method call with struct
parameters is too expensive. There are two alternatives:

1) Use a reference class instead of a struct. (This is only slightly
faster due to overhead of GC.)

2) Use a static method which accepts ref and out parameters rather than
relying on copying. E.g.:

public static void Add(out Scalar result, ref Scalar left,
ref Scalar right)

Both of these are faster to call than a static method relying on copies,
where the body simply returns an empty Scalar, for a Scalar containing 1
double, 1 integer and 1 enumeration value.

Don't forget that the more complex your method becomes, the less likely
the JIT compiler is to inline it.

-- Barry
Thanks for your input there, that helped big time. The C++ version
still blows it away (1.6ms VS 22ms for the identical # of operations)
but...I guess there is nothing further I can do performance-wise.

I am sure as the framework matures over the next years things like
that will speed up.

--
Stephan
2003 Yamaha R6

kimi no koto omoidasu hi
nante nai no wa
kimi no koto wasureta toki ga nai kara
Jul 6 '06 #3

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

Similar topics

4
2086
by: Mark Stijnman | last post by:
A while ago I posted a question about how to get operator behave differently for reading and writing. I basically wanted to make a vector that can be queried about whether it is modified recently or not. My first idea, using the const and non-const versions of operator, was clearly not correct, as was pointed out. Julián Albo suggested I could use proxies to do that. I've done some googling for proxies (also in this group) and personally,...
9
2860
by: Steve Sargent | last post by:
Hi: I'm trying to debug the following code, and it keeps looping on the if statement: public static bool operator == (OnlineMemberNode first, OnlineMemberNode second) { if(first == null) {
17
2513
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: public static bool operator !=(MyType x, MyType y) { return !(x == y); } That way the == operator handles everything, and extra comparing logic isn't
2
1717
by: Stig Nielsson | last post by:
I am wondering what is the most efficient way to make a parameterized factory method, and before I start spending time on performance measurements myself, I would like to hear the NG's opinion: I want my parameterized factory method to create either an object of type A or an object of type B, depending on the type of the Input parameter in the factory method. The type can be tested using the 'is' operator, but alternatively, the input...
9
1973
by: Anoj | last post by:
Hi All, is there any performance difference between + and & operator while concating string litrels. which one is better and why?? Thanx
9
4783
by: Marty | last post by:
Hi, Does using the the conditional operator (?:) instead of the common "if" statement will give a performance gain in a C# .NET 2003 application (even in C# .NET 2005?). What is the advantage of using it in C# other than typing shorter if ? Thanks, Marty
17
1924
by: Jedrzej Miadowicz | last post by:
I recently (re)discovered data binding in Windows Forms thanks to its advances in Visual Studio 2005. As I looked a little deeper, however, I realize that it still suffers from an irksome tendency to stick a whole bunch of literal strings in my code. Quite frankly, I consider them a plague imposed on developers by the RAD designers that come with Visual Studio. The problem with using literal strings to refer to controls, data sources,...
47
3373
by: Roger Lakner | last post by:
I often see operator implemented something like this: class Foo { ... }; class FooList { public: const Foo& operator (unsigned index) const {return array;}; Foo& operator (unsigned index) {return
12
2211
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid overloading of == and != but instead translate each call of objA==objB automatically in System.Object.Equals(objA, objB). This would remove inconsistencies like myString1==myString2
0
9706
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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
10075
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...
1
7615
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...
0
6851
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
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.