473,796 Members | 2,520 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can I resize an array?

Hi,

I declared an array like this:

string[] scriptArgs = new string[10];

Can I resize the array later in the code?

Thank you,
Max
Apr 12 '06
12 14458
> I redesigned an application that stored pixel
values of a camera in an ArrayList (.NET 1.1) of floats. The number of
varies from (320*240, 640*480, and 1024 * 1024). Performing calculations on
these pixel values stored in an ArrayList took many seconds, and sometimes
minutes. I converted the ArrayLists to arrays of floats and the calculations
now only take a couple of seconds.


Yes, but that's exactly my point. You had a slow application, you
determined where the slowdown was, and you optimized that section. My
point is that one shouldn't "optimize" before determining that there is
indeed a problem. In most cases, there won't be. In a few, such as
yours, there will be, and in those cases a change in data structure is
a good idea.

Too many programmers read that "boxing and unboxing are slow" and then
do horrible, tortured things to their code in order to avoid boxing and
unboxing at all costs, when in fact that's unnecessary in most cases.
It's far better to write the code using the data structure that makes
sense given the design, then profile the application, then make
adjustments where necessary, which is just what you did.

The only mistake that the original author of your application made was
that he or she didn't profile it to see if there was a problem area in
the code.

Apr 13 '06 #11
rmacias <rm*****@newsgr oup.nospam> wrote:
Even in .NET 1.1, though, boxing and unboxing are rarely so noticeable
that it's worth warping your design if an ArrayList was the "right
choice" from a design standpoint. Always choose the most appropriate
data structure for your design and then profile your app to see if
things like boxing cause you a real performance problem. Adapt your
design only if this is so. Otherwise you get tortured code in the name
of "efficiency " in spots where if you did it the nice (but
"inefficien t") way it would have almost no effect on your program
whatsoever.


I disagree about the performance hit of boxing/unboxing. For a small amount
of elements, there is a performance differents, but it is so small it is not
noticable. However, when you get into the thousands, and larger,
boxing/unboxing is noticable. I redesigned an application that stored pixel
values of a camera in an ArrayList (.NET 1.1) of floats. The number of
varies from (320*240, 640*480, and 1024 * 1024). Performing calculations on
these pixel values stored in an ArrayList took many seconds, and sometimes
minutes. I converted the ArrayLists to arrays of floats and the calculations
now only take a couple of seconds.

It really depends on the application. If you iterate through the array
once, the it doesn't make a difference. However, you have to access the
elements many times and interate through the arrays multiple times, then you
do notice a difference.


That's exactly what Bruce said though: write the code in the most
natural, readable way, then if there are performance problems, change
your app (after profiling) to fix those problems.

For "many times" and "multiple times" to actually make a significant
difference, you're likely to have to access millions of items (or the
same few items millions of times).

Even within the same app, there may be times where it's most
appropriate to use an ArrayList and times where an array would be
better. I totally agree with Bruce though - write the code to be
readable first, and worry about micro-optimisation later.

(One particular feature of your app sounds like you knew at runtime
what the size of the array would be before populating, so there
wouldn't be any need for resizing. When you know the size to start
with, an array usually ends up being easier to work with than an
ArrayList anyway.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 13 '06 #12
Bruce Wood <br*******@cana da.com> wrote:
So far as I know (and I'm often wrong) this is exactly what happens
with an ArrayList. ArrayList uses an array as its backing
implementation, so when you fill your ArrayList it simply allocates
another array and copies the contents from one to the other. I believe
that the size of the backing array doubles on each reallocation.

Of course, I could be completely out to lunch. Anyone know if this is
so?


Spot on.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 13 '06 #13

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

Similar topics

1
3729
by: Dean | last post by:
First I've must say Im completly new in php scripting What I need to do is upload, resize pictures with path in database Here is theory of it, and plan of doing it Hope somebody can help me here are 4 sizes of images:
16
6214
by: machine99 | last post by:
how do you resize an array allocated with new?
2
3503
by: xain | last post by:
HtmlString includes a web page, and soon it is converted to Html file. In the web page, they are images, and some of them are large. They are so large, in fact they are going to destroy my Tables, so I need to resize these images. I write a Function to do this job. Resizeimage(HtmlString) Function Resizeimage(ConStr) Dim TempStr,Re,Matches,Match,Tempi,TempArray
2
2666
by: mrbrightsidestolemymoney | last post by:
Hi, I'm having a problem resizing a (very big) nested vector. It's not the most streamlined piece of code ever but I need this array to avoid having to recalculate the same quantity millions of times! The (relevant) snippets of code are below : since it's relevant though L=28,T=96 (so TasteProps weighs in at a hefty 8*96*28*28*28*96=1,618,477,056 doubles )
7
6444
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are now thown out of the array released properly by the CLI?
6
13919
by: Jeff.Boeker | last post by:
I'm learning a lesson in how I need to be more specific :) In C++ I can resize a vector and it will allocate memory and it will call the default constructor if necessary (or I can supply an instance for the copy constructor). For example: C++ vector<classvClass;
2
2126
by: David Sanders | last post by:
Hi, I have a script with function definitions which I load into ipython for interactive use. These functions modify a global numpy array, whose size I need to be able to change interactively. I thus have a script which looks like this: from numpy import *
2
1567
by: Damien582 | last post by:
Hi guys, new here. I've used this site for a while researching answers, but this is my first post. I've searched everywhere and I haven't found an answer that works. First, what I am doing: Cubed array (3 dimesions, not jagged) of a custom structure for a game I am making to hold tile information. I can resize a 2 dimensional layer just fine, but upon resizing a 3 dimensional array (via creating new array and using Array.Copy) I could not...
9
6186
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I've an optimization question for you all really quick. I have a stream that I am reading some bytes. At times, the stream can contain a small amount of bytes such as 50 or so or it can contain as much 10000000 bytes. In reality, I do not know the maximum number of bytes. In my function, I am going to read() the byte stream using a buffer. Now, is it better to read it into a buffer and dump the buffer into a...
0
9528
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
10456
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
10230
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...
1
10174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7548
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
5442
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...
1
4118
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
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2926
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.