Usage of Array in C#.NET

  • Thread starter Thread starter Curious
  • Start date Start date
C

Curious

I have a colleague who used to program in S-PLUS. My impression about
S-PLUS is that it's based on matrix calculations and consumes a lot of
memory. It takes a long time to run a small program in S-PLUS.

Now he wants to learn C# in order to speed things up. He simply uses
Array in C#.NET (to resemble matrix in S-PLUS). He refers to
everything in the Array by indexes, i.e., integer numbers. It seems
fairly abstract.

I asked him to use objects and ArrayList. He is not used to the
concept of object-oriented programming. So he sticks to the Array with
those indexes. Each time when he asks me to help debug, it's a
headache because I have to remember the indexes that reference to
different things. All meaningless numbers pointed to by indexes.

A question for you folks: Is it a good idea to use Array? Wouldn't it
be better to use ArrayList instead?
 
I must say, I know nothing about S-Plus, and you don't really say what he's
storing in these arrays.
If he's just storing ints then why not use int[]? or at least List said:
He refers to everything in the Array by indexes, i.e., integer numbers.
I must be missing something here - how else would you index into an array?
Why do you think using an int to index into the array is wrong?

Cheers,

Adam.
 
Curious said:
I have a colleague who used to program in S-PLUS. My impression about
S-PLUS is that it's based on matrix calculations and consumes a lot of
memory. It takes a long time to run a small program in S-PLUS.

Now he wants to learn C# in order to speed things up. He simply uses
Array in C#.NET (to resemble matrix in S-PLUS). He refers to
everything in the Array by indexes, i.e., integer numbers. It seems
fairly abstract.

I asked him to use objects and ArrayList. He is not used to the
concept of object-oriented programming. So he sticks to the Array with
those indexes. Each time when he asks me to help debug, it's a
headache because I have to remember the indexes that reference to
different things. All meaningless numbers pointed to by indexes.

A question for you folks: Is it a good idea to use Array? Wouldn't it
be better to use ArrayList instead?

For matrix operations, performance is critical. Indexed array references are
typically the highest-performance way to do it, In general, this is a case
where object-oriented approaches just don't work out in practice because of
the inevitable overhead (please, no uninformed flames from the gallery on
this - it's well known to be true).

So if you're going to do his debugging for him, get comfortable with
multidimensional arrays. One thing you can do is ask him to rename the index
variables so that it's a little easier to visualize what the dimensions
mean.

However, indexed arrays are NOT necessarily the high-performance solution to
all problems he may be solving. There are places where the CLR types are
appropriate - for instance dictionaries for lookups, where the underlying
hashtable will beat a binary search on an array of keys.

It's not a certainty, by the way, that using C# will speed things up.
There's a good reason why Fortran, for instance, is still used heavily by
people who do compute-intensive math for a living. The optimizing
performance of Fortran compilers is legendary.

Good luck,
Tom Dacon
 
It's a financial application to read price and volume information for
a period of time for each security on the stock market from a huge
file.

Then based on the changes in price or volume during the recent days,
come up with a selection of securities.

I've created a program with a Security class, and ArrayList containing
a collection of Security instances. I've also created a method in the
Security class to do the calculation. Then I use Sort to sort the
ArrayList to come up with a list. It's fast, and straight-forward.

I still fail to understand why he doesn't accept the concept of object-
oriented programming.

Yes, he uses a lot of int[], double[], string[], etc. You have to go
to different Arrays to get different values for a same security. They
are not together.
 
Curious said:
It's a financial application to read price and volume information for
a period of time for each security on the stock market from a huge
file.

Then based on the changes in price or volume during the recent days,
come up with a selection of securities.

I've created a program with a Security class, and ArrayList containing
a collection of Security instances. I've also created a method in the
Security class to do the calculation. Then I use Sort to sort the
ArrayList to come up with a list. It's fast, and straight-forward.

I still fail to understand why he doesn't accept the concept of object-
oriented programming.

Yes, he uses a lot of int[], double[], string[], etc. You have to go
to different Arrays to get different values for a same security. They
are not together.

So profile your solution vs. his and see where the performance bottlenecks
are. If your object-oriented solution doesn't impose a significant penalty
(that is, your solution lies comfortably within the performance requirements
of the problem), then you've got a good case for using the CLR classes in
favor of his solution. Maintainability and understandability count for a
lot, as long as you haven't put a bullet in the head of the application
because of unacceptable performance.


Tom Dacon
Dacon Software Consulting
 
(e-mail address removed)>, (e-mail address removed)
says...>
I still fail to understand why he doesn't accept the concept of object-
oriented programming.

He's lazy, and he's got you. :(
 
Curious said:
I still fail to understand why he doesn't accept the concept of object-
oriented programming.

And I'm betting that the other programmer does not see why you want to
abandon S-PLUS. Until you show them why abandoning it 1) gives the same
answers, and 2) gets those answers faster, or more optimally, you've got
a hard sell.

Look at another point of view. They probably feel that their routine in
S-PLUS will not only solve your type of problem, but because the
matrices are generic, could also solve other problems. Your OO methods
and app are domain specific.
 
:-)

(please, no uninformed flames from the gallery on
this - it's well known to be true).


I remember a discussion where i told i favor hashtables for key value pair
retrieval
i was countered here with the argument that the generic framework classes
would be much faster etc etc etc

After biting back in that thread ( as i was getting the feeling i was
misunderstood as this person http://en.wikipedia.org/wiki/Louis_van_Gaal
( Dutch , German and Spanish people will recognize this ) ) it occured to
me that it is just a mather of how you define fast , fast in processing or
..... faster and more maintainnable to program

For the outsiders of football or europese champions league Mr van Gaal is
verry famous for his knowledge about coaching football ( he is pretty good
at it ) however he has a big flaw , he just asumes that evryone knows or
should know what he knows ( getting it :-) ) if a journalist asks him a
question that he feels is basic football knowledge he gets pretty upset and
becomes mad verry mad

At one time he countered a in his eyes stupid question with the famous
sentence "Am i so smart, or are you so dumb?" and remember that this was
on national televission :-)

I wish the Germans lots of luck with our Louis :-)


Michel
 
Back
Top