|
hi there,
i m reading a binary but it takes so long time to read, here is the codes
FileStream fs = new FileStream(@"D:\art1.bar", FileMode.Open);
BinaryReader br = new BinaryReader(fs);
string chk = "";
for (int a = 0; a < 4; a++)
{
chk += br.ReadChar().ToString();
}
int version = br.ReadInt32();
int unknown = br.ReadInt32();
byte[] uselessbytes = br.ReadBytes(264);
int unknown1 = br.ReadInt32();
int tnums = br.ReadInt32();
long dir = br.ReadInt64();
byte[] byts = br.ReadBytes(tnums);
br.BaseStream.Position = dir;
int dirnamelength = br.ReadInt32();
dirnamelength *= 2;
string dirname = "";
for (int a = 0; a < dirnamelength; a++)
{
dirname += br.ReadChar().ToString();
}
dirname = dirname.Replace("\0", "");
dirname = dirname.Replace("\\", "");
dirname = dirname.Replace(" ", "");
int numofFilesindir = br.ReadInt32();
for (int a = 0; a < numofFilesindir; a++)
{
dataGridView1.Rows.Add();
dataGridView1[1, a].Value = br.ReadInt32();
dataGridView1[2, a].Value = br.ReadInt32();
br.BaseStream.Position += 20;
int filelength = br.ReadInt32();
filelength *= 2;
string filename = "";
for (int b = 0; b < filelength; b++)
{
filename += br.ReadChar();
}
filename = filename.Replace("\0", "");
filename = filename.Trim();
filename.Remove(filename.Length - 3);
dataGridView1[0, a].Value = filename;
}
br.Close();
fs.Close();
it taking 15 seconds to read about 7000 files(i.e numofFilesindir = 7000)
i wanna know how can i reduce time or any alternative way that read fast
Becoming Programmer...
|
|
|
|
|
One reason is that you are concatening strings, this can become horribly slow inside loops, since each time a new string instance has to be created.
Use a StringBuilder instead and replace += with its Append method.
Also, you're reading the file byte by byte, why don't you read in all bytes at once an then convert the byte array to a string using the Encoding class?
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
ok i ll use string builder, and tell you the result
Greeeg wrote: Also, you're reading the file byte by byte, why don't you read in all bytes at once an then convert the byte array to a string using the Encoding class
i cannot read in my format, its a file format of a game, thats why i have to read like that
Becoming Programmer...
|
|
|
|
|
xmen_xwk wrote: i cannot read in my format, its a file format of a game, thats why i have to read like that
7,000 files for a game?? A little excessive, don't you think?
|
|
|
|
|
its a archive file where about 7000 files stored, and there are about 8 or 9 more files like that have lots of files stored
i think you never edit any game...
Greeeg Use a StringBuilder instead and replace += with its Append method.
i tried it too but no benefit
-- modified at 13:29 Sunday 16th September, 2007
Becoming Programmer...
|
|
|
|
|
xmen_xwk wrote: filename += br.ReadChar();
That's equivalent to:
object temp1 = (object)br.ReadChar();
string temp2 = fileName.ToString() + temp1.ToString();
fileName = temp2;
That means that you create three new objects for every single character that you read. If the filenames are 20 characters by average, that comes to 420000 objects for reading 7000 files.
Use the ReadChars method to read several characters from the stream.
xmen_xwk wrote: filename.Remove(filename.Length - 3);
That doesn't accomplish anything. The Remove method returns the new string, so you have to take care of the return value, just as you do with the Replace and Trim methods.
---
single minded; short sighted; long gone;
|
|
|
|
|
Guffa wrote: That doesn't accomplish anything. The Remove method returns the new string, so you have to take care of the return value, just as you do with the Replace and Trim methods.
but if i use replace then it replace all the "//" from string , i just want to remove this from last of string, not in all
Guffa wrote: Use the ReadChars method to read several characters from the stream
yeah i used that but its just return "System.Char[]" something like that
Becoming Programmer...
|
|
|
|
|
xmen_xwk wrote: i just want to remove this from last of string, not in all
That's just it. Your calling the Remove method, but completely ignoring the return from it. You can reomve the line completely from your code and nothing changes. It has to be:
filename = filename.Remove(something...);
|
|
|
|
|
xmen_xwk wrote: but if i use replace then it replace all the "//" from string , i just want to remove this from last of string, not in all
There's nothing wrong in using the Remove method, but you are using it wrong. Read what I wrote again, and see if you understand it.
xmen_xwk wrote: yeah i used that but its just return "System.Char[]" something like that
Then you don't use it correctly. It sounds like you are trying to use the ToString method on the array to get it as a string, but that will only get the type of the array. Use the string constructor that takes a char array.
---
single minded; short sighted; long gone;
|
|
|
|
|
ok i did some changes
1.
string filename = new string(br.ReadChars(filelength));
2.remove the line that was removing last 3 characters
but it still taking 12 secs, its too much for me, any other changes that reduce the time???
here is the changed coding
FileStream fs = new FileStream(@"D:\art2.bar", FileMode.Open);
BinaryReader br = new BinaryReader(fs);
string chk = new string(br.ReadChars(4));
int version = br.ReadInt32();
int unknown = br.ReadInt32();
byte[] uselessbytes = br.ReadBytes(264);
int unknown1 = br.ReadInt32();
int tnums = br.ReadInt32();
long dir = br.ReadInt64();
byte[] byts = br.ReadBytes(tnums);
br.BaseStream.Position = dir;
int dirnamelength = br.ReadInt32();
dirnamelength *= 2;
string dirname = new string(br.ReadChars(dirnamelength));
dirname = dirname.Replace("\0", "");
dirname = dirname.Replace("\\", "");
dirname = dirname.Replace(" ", "");
int numofFilesindir = br.ReadInt32();
for (int a = 0; a < numofFilesindir; a++)
{
dataGridView1.Rows.Add();
dataGridView1[1, a].Value = br.ReadInt32();
dataGridView1[2, a].Value = br.ReadInt32();
br.BaseStream.Position += 20;
dataGridView1[3, a].Value ="";
int filelength = br.ReadInt32();
filelength *= 2;
string filename = new string(br.ReadChars(filelength));
filename = filename.Replace("\0", "");
filename = filename.Trim();
dataGridView1[0, a].Value = filename;
}
br.Close();
fs.Close();
Becoming Programmer...
|
|
|
|
|
The DataGridView is probably the bottleneck now, I tried a basic example of adding 10000 rows, it was slow adding them in a loop like your example but when I added them using Rows.AddRange() it was much quicker.
DataGridViewRow[] rows = new DataGridViewRow[10000];
for (int i = 0; i < 10000; i++)
{
rows[i] = new DataGridViewRow();
DataGridViewCell cell1 = new DataGridViewTextBoxCell();
DataGridViewCell cell2 = new DataGridViewTextBoxCell();
DataGridViewCell cell3 = new DataGridViewTextBoxCell();
cell1.Value = i;
cell2.Value = i * 10;
cell3.Value = i * 100;
rows[i].Cells.AddRange(cell1, cell2, cell3);
}
dataGridView1.Rows.AddRange(rows);
You may want to look into the the virtual mode and/or the data sources that can be used with the DataGridView.
|
|
|
|
|
hey yeah it worked, time reduced to 3 secs or less
thank you so much;););););) and thanks to all who helped me in reducing time
Becoming Programmer...
|
|
|
|
|
Hi
I want to write a client/server program that can connect to each other in internet network.
Client can connect server and vise versa.
I know socket programming and know that I should have an ip and a port number but internet network have routers and different servers how I can find other(client or server) computer!?
Yahoo messenger is a good sample in my idea. User can connect to yahoo server from every where!?
how can yahoo massenger can do that?!
|
|
|
|
|
ali_reza_zareian wrote: User can connect to yahoo server from every where!?
how can yahoo massenger can do that?!
Ever heard of Multicasting?
Smile: A curve that can set a lot of things straight!
(\ /)
(O.o)
(><)
|
|
|
|
|
You have to tell the client what the IP of the server is. You really can't just broadcast "Hey, where are you??" and expect a response from a tiny little place in a network that spans millions of hosts.
ali_reza_zareian wrote: Yahoo messenger is a good sample in my idea. User can connect to yahoo server from every where!?
Because Yahoo messenger is using a well-known set of servers that don't change they're IP addresses.
|
|
|
|
|
Hi.
I have this problem.
In my project I have a control which does not fit into the screen. So there are scrollbars that user can use. My application also paints something direct on the control. And now I want to save the control's surface to the image file. I google this code:
[DllImport("gdi32.dll")]<br />
private static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, System.Int32 dwRop);<br />
[DllImport("User32.dll")]<br />
public extern static System.IntPtr GetDC(System.IntPtr hWnd);<br />
[DllImport("User32.dll")]<br />
public extern static int ReleaseDC(System.IntPtr hWnd, System.IntPtr hDC);<br />
<br />
private void foo() {<br />
System.IntPtr srcDC = GetDC(this.ctrl.Handle);<br />
Bitmap bm = new Bitmap(this.ctrl.Width, this.ctrl.Height);<br />
Graphics g = Graphics.FromImage(bm);<br />
System.IntPtr bmDC = g.GetHdc();<br />
BitBlt(bmDC, 0, 0, bm.Width, bm.Height, srcDC, 0, 0, 0x00CC0020 );<br />
ReleaseDC(this.ctrl.Handle, srcDC);<br />
g.ReleaseHdc(bmDC);<br />
g.Dispose();<br />
bm.Save("a.bmp");<br />
}
But there is a problem because it capture only the visible part of the control. And I want to capture it all (also the part that is not visible due to actual scroll position).
Could you please help me what I can do?
0101000101110101
0110000101110011
01101111
|
|
|
|
|
quaso1 wrote: But there is a problem because it capture only the visible part of the control.
Yep. You cannot capture what you cannot see, simply because Windoes does usually waste the time drawing it.
quaso1 wrote: My application also paints something direct on the control.
This is the wrong approach. If you're going to draw something, draw it to a Bitmap object in memory, then you can draw that image to the visible part of the control in the control's paint event. If you need to save that image, just use the Save methods of the Bitmap object.
|
|
|
|
|
Well its rather complicated
There are also other usercontrols added to the component and I want to capture them also. I want to capture all the graphics and components on that control.
As I wrote, the code above just do it, but only the visible part of the parent control.
0101000101110101
0110000101110011
01101111
|
|
|
|
|
You really don't have any choice in the matter. You can only capture what you can see.
|
|
|
|
|
I was looking for a way to get the temperature of my CPU and possibly the temperatures of my HD:s
To do this I've been playing around with WMI, the "Win32_TemperatureProbe" with "CurrentReading" seemed like a good place to start but I just cant get any value from it.
I got the code below from MS WMI Code Creator but it doesn't seem to work.
Am I going at this the wrong way and should I try something else?
using System;<br />
using System.Management;<br />
using System.Windows.Forms;<br />
<br />
namespace WMISample<br />
{<br />
public class MyWMIQuery<br />
{<br />
public static void Main()<br />
{<br />
try<br />
{<br />
ManagementObjectSearcher searcher = <br />
new ManagementObjectSearcher("root\\CIMV2", <br />
"SELECT * FROM Win32_TemperatureProbe"); <br />
<br />
foreach (ManagementObject queryObj in searcher.Get())<br />
{<br />
Console.WriteLine("-----------------------------------");<br />
Console.WriteLine("Win32_TemperatureProbe instance");<br />
Console.WriteLine("-----------------------------------");<br />
Console.WriteLine("CurrentReading: {0}", queryObj["CurrentReading"]);<br />
}<br />
}<br />
catch (ManagementException e)<br />
{<br />
MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);<br />
}<br />
}<br />
}<br />
}
|
|
|
|
|
|
The problem with using WMI is that the manufacturers are not obligated to write providers for it. So, even though the class exists, there's no data to get because the manufacturer of the MoBo hasn't provided any drivers for WMI.
|
|
|
|
|
Maybe you could use the Tool SpeedFan and get the value from it.
I'm using SpeedFan and LCD Studio to show the temperatures of my system on my G15 keyboard.
-^-^-^-^-^-
no risk no funk ................... please vote ------>
|
|
|
|
|
I've been using SpeedFan and Everest but I was just curious to see if it could be done with .NET, but I guess it's more trouble than it's worth
|
|
|
|
|
hi
i want to use ngen to create native image, but i don't know how to use it and where native image of my application save and some information about it.
thanks.
|
|
|
|