Click here to Skip to main content
15,915,093 members
Home / Discussions / C#
   

C#

 
GeneralRe: Accessing resource on unknown CD-ROM (drive letter). Pin
TrooperIronMan13-Jan-07 9:47
TrooperIronMan13-Jan-07 9:47 
GeneralRe: Accessing resource on unknown CD-ROM (drive letter). Pin
TrooperIronMan13-Jan-07 13:41
TrooperIronMan13-Jan-07 13:41 
Questionhow to configure msdn in my vs2005 correctly ? Pin
hdv21213-Jan-07 7:44
hdv21213-Jan-07 7:44 
AnswerRe: how to configure msdn in my vs2005 correctly ? Pin
Ed.Poore13-Jan-07 7:54
Ed.Poore13-Jan-07 7:54 
AnswerRe: how to configure msdn in my vs2005 correctly ? Pin
Thomas Stockwell13-Jan-07 16:30
professionalThomas Stockwell13-Jan-07 16:30 
QuestionPrimitive types or object types what to prefer Pin
ComCoderCsharp13-Jan-07 7:21
ComCoderCsharp13-Jan-07 7:21 
AnswerRe: Primitive types or object types what to prefer Pin
Stefan Troschuetz13-Jan-07 8:30
Stefan Troschuetz13-Jan-07 8:30 
AnswerRe: Primitive types or object types what to prefer Pin
Luc Pattyn13-Jan-07 8:50
sitebuilderLuc Pattyn13-Jan-07 8:50 
Hi,

A ComplexNumber class would use 16 bytes to store two 4B numbers (real+imag parts) plus 8B of
administrative stuff. So it effectively doubles the memory requirements, which
is only really significant if you are going to need more than 1M of those numbers.

I am not sure whether a struct has the same memory overhead or not.
But this is simple to find out: just create a small program than creates 1M complex numbers
(as 2 floats, as a struct, as a class) and in each case compare its memory footprint
before and after creation (make sure the objects are still alive when you measure (e.g.
by including code to sum them up, then print the sum).

The calculation cost will be different too. With a class, the natural way would be
to write methods that return a new object (say c=a+b), which involves calling the method,
doing very simple arithmetic, creating a new object, and returning it. (And this is
what you would have to do if you want to overload the + - * operators)

A cheaper approach would be to provide methods that do not return results, but modify
their parameters, something like:
static void AddTo(ComplexNumber a, ComplexNumber b); // performs a=a+b

of course, if ComplexNumber is a struct, you would use ref everywhere (obviously for
in/out values, but I recommend it for input-only parameters too).

The cheapest, but hardest way, would be to forget about a complex class/struct and
write everything in real numbers, with explicit add/multiply code all over the place.
Completely unstructured, very error prone and the fastest (at run-time that is).

If your application needs a lot of complex functions (hence a lot of code), I would
go for a class with overloaded operators, so you can really write c=a+b with abc all complex.

If the number of formulas is limited, I would suggest you start with a ComplexNumber struct
and use explicit method calls for each operation; but avoid creating objects all the time,
and certainly do not rely on methods that return a struct: instead have them modify an
already existing struct.

If in the end you are still convinced it needs to be faster and/or smaller, you could
then revisit the initial choice. But dont start it in the hard way from day 1.
But I do not expect you to really need this, unless you are chasing the next Mersenne prime
or so...


Hope this is helpful.

Smile | :)


Luc Pattyn

GeneralRe: Primitive types or object types what to prefer Pin
ComCoderCsharp13-Jan-07 10:51
ComCoderCsharp13-Jan-07 10:51 
GeneralRe: Primitive types or object types what to prefer Pin
Kevin McFarlane13-Jan-07 11:26
Kevin McFarlane13-Jan-07 11:26 
GeneralRe: Primitive types or object types what to prefer Pin
Scott Dorman14-Jan-07 3:38
professionalScott Dorman14-Jan-07 3:38 
GeneralRe: Primitive types or object types what to prefer Pin
Luc Pattyn14-Jan-07 3:55
sitebuilderLuc Pattyn14-Jan-07 3:55 
GeneralRe: Primitive types or object types what to prefer Pin
Scott Dorman14-Jan-07 4:15
professionalScott Dorman14-Jan-07 4:15 
GeneralRe: Primitive types or object types what to prefer Pin
ComCoderCsharp14-Jan-07 4:54
ComCoderCsharp14-Jan-07 4:54 
GeneralRe: Primitive types or object types what to prefer Pin
Scott Dorman14-Jan-07 5:18
professionalScott Dorman14-Jan-07 5:18 
GeneralRe: Primitive types or object types what to prefer Pin
ComCoderCsharp14-Jan-07 5:41
ComCoderCsharp14-Jan-07 5:41 
GeneralRe: Primitive types or object types what to prefer Pin
Scott Dorman14-Jan-07 5:47
professionalScott Dorman14-Jan-07 5:47 
AnswerRe: Primitive types or object types what to prefer Pin
Luc Pattyn14-Jan-07 12:14
sitebuilderLuc Pattyn14-Jan-07 12:14 
QuestionC# or C++ and why ? Pin
Software_Specialist13-Jan-07 5:54
Software_Specialist13-Jan-07 5:54 
AnswerRe: C# or C++ and why ? Pin
Ed.Poore13-Jan-07 7:56
Ed.Poore13-Jan-07 7:56 
AnswerRe: C# or C++ and why ? Pin
Colin Angus Mackay13-Jan-07 9:20
Colin Angus Mackay13-Jan-07 9:20 
GeneralRe: C# or C++ and why ? Pin
Software_Specialist13-Jan-07 22:43
Software_Specialist13-Jan-07 22:43 
GeneralRe: C# or C++ and why ? Pin
Nadia Monalisa13-Jan-07 23:45
Nadia Monalisa13-Jan-07 23:45 
GeneralRe: C# or C++ and why ? Pin
Colin Angus Mackay14-Jan-07 3:26
Colin Angus Mackay14-Jan-07 3:26 
QuestionA user-interface question Pin
Zerox MXI13-Jan-07 1:59
Zerox MXI13-Jan-07 1:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.