|
Luc Pattyn wrote: Sure, for ease of use I would implement ComplexNumber as a class or struct, with operators
overloaded.
I guess your original post didn't seem clear to me.
Luc Pattyn wrote: For performance reasons I would rather not use classes (too many objects), and not
return structs (too many data moves; for 8B of data it is debatable, for large structs
I certainly dont want them returned), hence ref struct, hence no operator overloads.
From the MSDN[^] docs:
A struct can be considered a lightweight class, ideal for creating data types that store small amounts of data, and does not represent a type that might later be extended via inheritance.
I don't see how the performance of a struct is going to be detrimental if the "rules" are followed. Again, all of the native data types are implemeneted using structs and there aren't any perforamnce issues with them.
-----------------------------
In just two days, tomorrow will be yesterday.
|
|
|
|
|
Hi again
I have read a bit up on which types to prefer and it seems to me that defining a struct and using it as a primitive type, would be the same as just using primitive types which are structs themself. This means that if I want to represent some complex data (Re,Im). I could do it using two e.g float arrays representing the real part and the imag part, but it would be the same, if I used a struct called Complex(float,float).
In other words:
float[] = realdata;
float[] = Imagdata;
would be the same as:
Complex[,] = new Complex(realdata,Imagdata);
Right?
All the best
T
|
|
|
|
|
You are essentially correct. The benefit to defining your own struct is that you can then ecapsulate the two parts of a complex number and define the arithmetic rules (+, -, etc.) which allows you to treat the complex number as if it were a native data type.
I'm not sure why you would want to use arrays in the way you are showing. Defining a ComplexNumber struct would allow you to do something like this:
ComplexNumber c = new ComplexNumber(real, imaginary);
Console.WriteLine("Complex Number Real: {0}, Imaginary: {1}", c.Real, c.Imaginary);
ComplexNumber d = new ComplexNumber(real2, imaginary2);
ComplexNumber e = c + d;
Console.WriteLine("Complex Number Real: {0}, Imaginary: {1}", e.Real, e.Imaginary);
The benefit here is that the ComplexNumber knows how to add two complex numbers together and the calling code doesn't need that complexity.
If you wanted to use this in an array, it would be something like this:
Complex[] complexArray = new Complex[3];
complexArray[0] = new ComplexNumber(real, imaginary);
complexArray[1] = new ComplexNumber(real2, imaginary2);
complexArray[2] = new ComplexNumber(real3, imaginary3);
-----------------------------
In just two days, tomorrow will be yesterday.
|
|
|
|
|
Hi again
Thanks for the answer.
I agree on what you are saying, this is how I have done it through out my project. I just wanted to know if there possible was a better way. My concern was that when I use a Complex array of e.g 100000 values and every time using the "new" inside a loop and thereby assigning a new memory location every time, would lead to a unnessecery usage of memory. So initially I was wondering if this could be avoided using primitive types. But this does not seeme to be the case - I think
All the best
T
|
|
|
|
|
No matter what, you will be allocating memory in every iteration of the loop. The only difference is that you would allocating memory to hold a ComplexNumber struct as opposed to individual native data types. Because structs are a value type, they handle memory a bit differently than classes. The biggest difference is that structs are stored on the stack and are collected shortly after that stack frame goes out of scope. The other big difference is that structs actually store their values rather than references to them.
-----------------------------
In just two days, tomorrow will be yesterday.
|
|
|
|
|
Hi,
Having read lots of considerations, some experiments seemed overdue.
So I developed a little test to actually see memory footprint and execution times
for the different complex number approaches.
My test first allocates and initializes 5 million complex numbers stored
- as two float arrays
- as one array of class ComplexClass instances
- as one array of struct ComplexStruct
as expected the numbers were 40MB, 100MB and 40MB respectively,
proving the class instance takes 8B of overhead plus 4B for the pointer (32bit Windows),
the struct has no memory overhead.
Then I continued to measure the time required to square these numbers; I did 5M multiplies,
involving only the first 16K of the numbers in each array (in order to avoid cache misses),
and measured elapsed time.
A release build with Visual 7.1 (.NET 1.1) and Visual 8.0 (.NET 2.0) respectively,
running on a Pentium M at 1.7GHz, performing multiple passes, yielded the following numbers:
.NET 1.1 .NET 2.0
floats 4110 msec 3970 msec
class 4480 msec 4330 msec
structs 4150 msec 4080 msec
ref structs 4060 msec 3970 msec
The good news here is performance is very much the same in all cases (slightly
over 1M mul/sec); the class is some 10% slower, and the ref struct is the best;
also NET2.0 is marginally faster than NET1.1 here.
Conclusions:
- struct is cheaper than class in RAM and cycles (as expected)
- ref struct is faster than struct (as expected) but only marginally so
- the hard way does not bring anything with respect to ref structs (the positive surprise).
So whenever I would need complex numbers, I would go for structs.
The compiler seems good enough to make the hard way pointless.
Cheers
Luc Pattyn
|
|
|
|
|
Hi All,
why C# is better than C++, if not then why?
Can we perform every task with C# that we can with C++...?
Can we create a GAME OR GAME ENGINE using C# like we can with C++..?
I am bit new in programming world, so curious to know the difference in real world...
Thanks in advance
|
|
|
|
|
Software_Specialist wrote: why C# is better than C++, if not then why?
Depends on the context it's being used in.
Software_Specialist wrote: Can we perform every task with C# that we can with C++...?
Most of them if not all.
Software_Specialist wrote: Can we create a GAME OR GAME ENGINE using C# like we can with C++..?
Yes
Software_Specialist wrote: I am bit new in programming world, so curious to know the difference in real world...
Google?[^]
I have no idea what I just said. But my intentions were sincere.
|
|
|
|
|
Software_Specialist wrote: why C# is better than C++, if not then why?
It isn't better or worse. It is just different. C# is better for business applications mainly because the framework supports that kind of application. Of course other frameworks are available XNA is designed for games programming.
Software_Specialist wrote: Can we perform every task with C# that we can with C++...?
No. It is next to impossible to write a good/fast driver in C#.
Software_Specialist wrote: Can we create a GAME OR GAME ENGINE using C# like we can with C++..?
Yes. See XNA.
|
|
|
|
|
|
Hi Colin
Colin Angus Mackay wrote: No. It is next to impossible to write a good/fast driver in C#.
I have something to say about it. Did you mean, C# is not good for making driver or .NET language is not good for making driver ? Because what I thought was, every .NET language is translated to common language runtime and do the work in same performance for the target operating system. So, if I choose Visual C++.NET to make a driver, I did not think that it would be any better than Visual C#.NET. Unless if I dont make the driver in some other C++ (like borland etc) targeting native Operating System directly bypassing any CLR. Ain't I right ?
Thanks again for your valuable information.
Emran
|
|
|
|
|
bashiwala wrote: .NET language
.NET is not a language. It is a framework.
I did mean C# and my comment can be extended to all languages that compile to MSIL. However, since this is posted in the C# forum I restricted my answer to that domain.
bashiwala wrote: So, if I choose Visual C++.NET to make a driver, I did not think that it would be any better than Visual C#.NET.
C++ with managed extensions has a slight advantage over C# in that it can move between the managed and unmanaged world more easily. In C# you have to declare blocks of code and unsafe and it begins to look ugly after that.
bashiwala wrote: Unless if I dont make the driver in some other C++ (like borland etc) targeting native Operating System directly bypassing any CLR. Ain't I right ?
Sorry, you've lost me. That doesn't make any sense to me.
If you are creating a driver I suggest you don't use something that targets the CLR. It isn't a case of bypassing the CLR because the CLR is not part of the kernal in the OS, where most drivers reside.
|
|
|
|
|
Hi there,
I am looking to code or use a control or UI widget that would allow me to display real-time information when I mouse hover certain controls on a form. I tried to use ToolTip but it doesn't update in real-time, but only gives you a snapshot of the text you have updated. Anyway... I guess a Tooltip isn't a really good UI control to display or report real-time figures. But it's along that line, when I mouse hover, it shows up a box or something like that and displays real time information, meaning it would graphically update when my mouse is still hanging over the control I want to display information about.
Any kind dude please give me some suggestions? Thanks...
Zerox
|
|
|
|
|
Try searching the site for SuperToolTips. The one that I remember mimics the interface of Office 2007 and allows you to have a custom tooltip that can have a title, piture, color scheme, text, help button, etc. It may help.
Regards,
Thomas Stockwell
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Visit my homepage Oracle Studios[ ^]
|
|
|
|
|
I have a program with
textBox1 , textBox2 and button1 .
And my friend have that program.
How to do this:
When i insert in textBox1 his IP address, and insert some text in textBox2 , when i click on button1 to send him the text entered in textBox2 .
How to establish a communication like this.
I have Visual Studio 2003.
Vasildb
|
|
|
|
|
If the two computers are part of the same network you can use the API command NetSend. The command just takes the paramters of the ip address and the string to send.
Regards,
Thomas Stockwell
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Visit my homepage Oracle Studios[ ^]
|
|
|
|
|
Hi,
I have 2 classes
1. Race
2. RunningRace.
The first one is the base class and the other one Inherits from the first.
I need to add method "ComperTo" for each of the classes - - so i add Inherits of the interface IComparable for each of those classes.
My question are
1. Is there is other way to do it ?
2. I have 2 method with same name in this case and i cant add 'Virtual' keyword to any method so i have some problem that i don't know how to solve ...
3. If the base class Inherits interface - is the class that Inherits from him also must add the pure virtual method ?
Thanks for any help.
|
|
|
|
|
Hi,
Add the "CompareTo" only to the Race class.
Declare it as a virtual method (Just add the virtual keyword to its declaration).
Provide an overrding to the "CompareTo" method in the RunningRace class.
<br />
public class Race : IComparable<br />
{<br />
...<br />
public virtual int CompareTo(object obj){...}<br />
...<br />
}<br />
public class RunningRace : Race<br />
{<br />
...<br />
public override int CompareTo(object obj){...} <br />
...<br />
}<br />
Uri
|
|
|
|
|
Toda Gever.
|
|
|
|
|
Hi, I want that when my service installs, an icon appear in the notification area.(the installation starts the service).
I've arrived to do this.
But now, I want that the icon appears on start; and on stop, it changes without desappearing.
It appears on start but on stop, it disappears.
This code is in the OnStop() event
{
notifyIcon.Icon = iconStop;
}
Any Idea?????????
|
|
|
|
|
balboul wrote: Any Idea?????????
When your service stops the executable is unloaded, along with the icon. If you want the icon to remain even while the service is stopped then you need a secondary application that manages just the icon.
|
|
|
|
|
Please can you give me more information?
How to do the second application and to related it to the service.
|
|
|
|
|
I created a help file in windows workshop and that file i want to access through C#.Net. I used the web browser control and access it. file also open but it will give message that "You want to save or open the file".
that i don't want. I want that help file directaly open.
so if you have any other idea then please tell me
-- modified at 5:20 Saturday 13th January, 2007
|
|
|
|
|
Take a look at the HelpProvider component.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
hi
I want to draw 2 or more circles and determine if they crossed
if they crossed i want not draw the inter section lines
any help??????????
|
|
|
|