|
I always meet the situation in my code. For example when filling array while parsing a text file or reading Sql query result.
I used two solutions to this:
1. at first I fill ArrayList, then copy items to the array with help of CopyTo method of ArrayList
<code>
ArrayList arr = new ArrayList();
while(reader.Read())
{
arr.Add(reader.GetString(0));
}
string[] strs = new string[arr.Count];
arr.CopyTo(strs);
... do sth
return strs;
</code>
2. create array of maximum expexcted length and then copy items to new array when first array is filled and the length is known
<code>
string[] strs = null;
using(string[] tmpArray = new string[MAX_POSSIBLE_LENGTH])
{
int i = 0;
while(reader.Read())
{
tmp[i] = reader.GetString(0);
i ++;
}
strs = new string[i];
Array.Copy(tmpArray, strs, i);
}
... do something
return strs;
</code>
Does somebody know the better solution? If there is not one which of the above is better?
Evgeny Pokhilko
C# .NET monomaniac
|
|
|
|
|
The first is obviously better. There's no other way, an ArrayList will allow you to change the size, an Array will not. I assume you're not using .NET 2.0, if you are, don't use arraylist.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Hi there,
We're having a small test here at work. Read in a text file and convert half the text to uppercase as quickly as possible ("aabb" => "aAbB", so not "AAbb")
So I read in the text file and do a for with i+=2 instead of i++ and setting a Stringgbuilder. after the text is done I put text into the text box. Then I use ngen to boost speed a bit (compiler is set to optimized etc.) I also tried to use SuspenLayout and ResumeLayout, but that didn't gain me a lot.
Any other tricks I can use to boost this up? (maybe I forgot to set some compiler options or I have to do it in another way?
here's my code:
System.IO.StreamReader reader = new System.IO.StreamReader(openfiledlg.FileName);
text = new StringBuilder(reader.ReadToEnd());
reader.Close();
pb_conversion.Maximum = text.Length;
starttime = DateTime.Now;
for(int i = 1; i < text.Length; i+=2){
text[i] = text[i].ToString().ToUpper()[0];
pb_conversion.Value = i;
}
txtbox_result.Text = text.ToString();
difference = DateTime.Now - starttime;
lbl_result.Text = "Done in: " + difference.TotalMilliseconds + " milliseconds.";
lbl_result.Visible = true;
I'm curious what else I could do...
thanks.
V.
I found a living worth working for, but haven't found work worth living for.
|
|
|
|
|
You could go through the text as a char array and just subtract 32 from each other char to get uppercase.
Might be a little faster.
|
|
|
|
|
I reckon the ToString is probably expensive. Just deal with chars, instead. You can use Char.IsLower to find out if a char is a lower case letter, or just use Char.ToUpper to convert it.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
thanks for the reply. I'm around 81 seconds with a 20 Mb file. (84 first)
|
|
|
|
|
I had a play round with this, and got a 21 MB file to process in about 10 seconds. This code isn't perfect, but it is a decent basis to carry on with:
public void ReadFile()
{
if (openfiledialog.ShowDialog() == DialogResult.OK)
{
System.IO.StreamReader reader = new System.IO.StreamReader(openfiledialog.FileName);
StringBuilder text1 = new StringBuilder(reader.ReadToEnd());
reader.Close();
pb_conversion.Maximum = text1.Length;
DateTime starttime = DateTime.Now;
int innercount = 0;
for(int i = 1; i < text1.Length; i+=2)
{
text1[i] = text1[i].ToString().ToUpper()[0];
if (++innercount == 1000)
{
innercount = 0;
pb_conversion.Value = i;
}
}
txtbox_result.Text = text1.ToString();
TimeSpan difference = DateTime.Now - starttime;
MessageBox.Show(difference.ToString());
}
} One of the key areas is that you no longer attempt to update the progress bar every iteration. This is just wasteful and should be avoided - which is why I update every 1000 iterations.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
dude,
I couldn't believe my eyes when I saw the difference
11 seconds !
thanks man.
|
|
|
|
|
Glad to be of service.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Wouldn't using a char[] be faster than using a StringBuilder?
<br />
char[] c = sr.ReadToEnd().ToCharArray();<br />
<br />
for (int i = 0; i < c.Length; i+=2)<br />
{<br />
c[i] = (char)((int)c[i] - 32);<br />
}<br />
|
|
|
|
|
It would, but you need to enclose the c[i] test with an if (char.IsLetter(c[i])... so that you don't end up converting none alphabetic characters.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Hello,
I want to copy a string into a byte[] array.
string has a method ToCharArray that returns a char[] but that`s just not good enough, since a char is 2 bytes.
Is there a quick and easy way to accomplish this? In unmanaged C++ I could just do a memcpy or memmove into a designated BYTE array, is there something similar I can use in C#?
Thanks for your feedback,
Davy
|
|
|
|
|
byte[] mybyte = Encoding.ASCII.GetBytes("Hello World");
|
|
|
|
|
byte[] bytes = System.Text.Encoding.ASCII.GetBytes("MyString");
where ASCII can be replaced with other available encodings.
|
|
|
|
|
System.Text.Encoding.
A byte sequence means nothing without the information on how to map the byte sequence to characters. This is what the Encoding classes do.
|
|
|
|
|
Thanks all
That`s what I was looking for!
|
|
|
|
|
hi everyone,
I am working in a C# 2.0 Windows Aplication and it had a menu strip. I made a function to hide some of its MenuItems under certain circumstances. the Function was some thing like this
public static void Hide_MenuItems(MenuStrip menustrip)
{
try{
menustrip.Items["productsMenuStipItem"].Visible = false;
menustrip.Items["productreportsMenuStipItem"].Visible = false;
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Now that code is giving a Null reference exception on productreportsMenuStipItem which is an item that come under the MenuItem like this Products--->Product Reports.
So far I'm not able to understand why this is happening coz when I write this
productreportsMenuStipItem.Visible = false;
in the form that has that menuStrip it working perfectly alright but when I try to do that in this function it gives out a nullReferenceException. It seems as if the only Menuitems tranfered are the ones that are at level zero. like that productsMenuStripItem.
Can u plz give me a clue so that I can hide the menuStipItems with the help of this function? or atleast explain why this is happening?
thanks in advance
Rocky
|
|
|
|
|
I am guessing this has to do with nested menu items. It seems like you are hiding the parent item so the children items aren't there. I am not sure why you are doing it this way. You can access the object directly.
this:
productreportsMenuStipItem.Visible = false;
is not the same as this:
menustrip.Items["productreportsMenuStipItem"].Visible = false;
One you are marking the menu item itself as not visible. Then other you are using the Menustrip to access the menuitem. I would suggest always accessing the menustripitem directly and I think you won't have any problems.
Hope that helps.
Ben
|
|
|
|
|
Hello,
I have a long treeView, how can I scroll to the top of the control, without selecting anything?
Please help.
|
|
|
|
|
|
well as far as my knowledge goes, I think its impossible really. Think some other way instead of this idea. As far as I see it Crystal reports doesnt allow you to access its inner details via programming.
Rocky
|
|
|
|
|
Hi Everyone,
here i am with really a mystic question.
the scenario is:
-=-=-=-=-=-=-=-=-=-
I have a repeater and a checkbox.The checkbox is databounded in the repeater.There are 4 rows in the database.when the page is loaded,the 4 checkboxes are created dynamically w.r.t rows in table.When the check box is checked,the corresponding id's of the checkboxex are to be captured in an event.
And My query is:
-=-=-=-=-=-=-=-=-=-
how can we get the ID's of the corresponding checkbox when the checkbox is checked?
thanks
tirumal.
-- modified at 6:37 Tuesday 24th April, 2007
|
|
|
|
|
how to create a error log files?
how to handle the user exceptions while run the web application?
|
|
|
|
|
you handle the user exceptions using the traditional try/catch/finally(optional) block.
Keshav Kamat
India
|
|
|
|
|
i actually did not get you properly.
do you mean to say, you want to make sure that the user does not enter wrongly or something.
in that case you want something called validation.
You need to check out key press event in that case.
Keshav Kamat
India
|
|
|
|