|
Additionally, don't do for (int w = 0; w < bmp.Width; w++) , instead do:
int wid=bmp.Width;
for (int w=0; w<wid; w++)
This avoids bmp.Width to be calculated for every pixel, and results in a significant
speed gain (that is once you did everything else correctly, with lockbits, unsafe,
a pointer, etc. it will make a noticable difference in debug mode, not sure about
release mode).
-- modified at 18:11 Thursday 9th August, 2007: added debug/release nuance.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Dont use getpixel, it's slow. use lockbits instead and iterate trough the image.
http://www.codersource.net/csharp_image_processing.aspx
(look for Iterating through the Image header ;) )
one easy. probably not correct way to calc the amount of red would be to
have three variables, Red,Green and Blue and add the r, g and b value to them for each pixel.
red / (red+green+blue)=percentage of red..
hmm. staring at that makes me doubt it would produce correct numbers.
red would be 33.nnn% in grayscale image
hope it helps. may i ask what you're doing?
If it's stuck, DO NOT pull harder!
|
|
|
|
|
I wonder ... Maybe someone has an idear about this problem . . .
I have created a listview with 12 columns. When I select say 100 rows it takes about 2-3 seconds to copy them to the clipboard (for copying them to excel):
string sClipBoard = "";
for (int i = 0; i < listView1.SelectedItems.Count; i++)
{
sClipBoard = sClipBoard + listView1.SelectedItems[i].SubItems[1].Text + "\t" +
sClipBoard = sClipBoard + listView1.SelectedItems[i].SubItems[2].Text + "\t" +
etc.
}
finally: Clipboard.SetDataObject(sClipBoard, true);
OK. That works fine with 100 lines except: 200 l=9s 300l=20s 400l=35s 1000l= 3m30s
Replacing the string with a stringbuilder does not effect the speed.
Splitting the action in numbers of 100 does not effect the speed (neither different strings)
There has to be a better way ?
Any suggestion is welcome Thanks
|
|
|
|
|
First, figure out where your bottleneck is. If you comment out the Clipboard.SetDataObject call, is it still slow?
Here's my guess: all that string concatenation is killing performance. I'd try something like this:
StringBuilder builder = new StringBuilder(listView1.SelectedItems.Count * 50);
foreach (ListViewItem currentItem in listView1.SelectedItems)
{
builder.Append(currentItem.SubItems[1]);
builder.Append("\t");
builder.Append(currentItem.SubItems[2]);
}
finally: Clipboard.SetDataObject(builder.ToString(), true);
|
|
|
|
|
well the bottleneck seems to be the adding of the subitems.
If i leave out the clipboard the speed does not change.
If I just select subitems[1] it takes about 20 seconds for 1000 rows . . .
|
|
|
|
|
The string concatenation has to be the problem. Did you try this?
StringBuilder builder = new StringBuilder(listView1.SelectedItems.Count * 50);
foreach (ListViewItem currentItem in listView1.SelectedItems)
{
builder.Append(currentItem.SubItems[1]);
builder.Append("\t");
builder.Append(currentItem.SubItems[2]);
}
Clipboard.SetDataObject(builder.ToString(), true);
|
|
|
|
|
thanks for the quick reply, just built it in, for 400 rows it took 34 sec (-1)
For 1000 rows 3m43 (+13)
|
|
|
|
|
My mistake ... amazing ...
I took your last source but used the:
for(i=0; i< listview1 etc )
to correct it for a proper comparrising, i used the foreach routine
it takes for a 1000 rows about 1 second ??? for 3000 a second ???
WOW
and THANKS!!! i was 'rumbling' (if that's a correct word) with it for a couple of weeks
|
|
|
|
|
Interesting, however I'm a bit skeptical. Unless the listView.SelectedItems[index] makes some calls into native code, the for(...) loop should be just as fast -- if not slightly faster -- than foreach. You're sure the for(...) loop was slow even with the StringBuilder? I'd be really surprised if this was the case.
|
|
|
|
|
Actually it all depends on the cost of the termination test in the for loop;
that test gets executed for each iteration, if it entails a method or property lookup,
even when we know the result is or should be constant, it does not get optimized
and moved outside the loop, so it is executed over and over.
The biggest performance mistake in image processing is writing
for (int i=0; i<bitmap.Width; i++) for (int j=0; j<bitmap.Height; j++) ...
since that will probably spend more time interrogating the bitmap's boundaries
size than it does dealing with the actual pixels.
Another big victim is RichTextBox when looping over the text, testing for Text.Length,
which would recalculate the text over and over (and that's why I guess they also offer RichTextBox.TextLength).
-- modified at 17:50 Thursday 9th August, 2007 (patched the less-than sign)
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Luc,
I've been told by people smarter than myself that the JIT compiler will optimize out things like that which can be determined to not changed for the duration of the loop. Seems like this would be one of those cases...
|
|
|
|
|
Judah, I know from experience in debug mode it is not optimized out; I am not sure
what the release situation is. I'll watch it when opportunity knocks.
On the same subject, do you know of an easy way to watch the assembly code generated
by JIT both in debug and in release conditions when programming in C# (using VS Express
Edition). I understood C++ is willing to show a Disassembly window (after setting a
breakpoint?) but C# is not according to this[^]
I would appreciate any means to have a peek at it, even through a complex way; but
obviously, easier would be better.
TIA
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Luc, I'm afraid I don't know how to do this.
FWIW, here's a detailed discussion about the assembly generated from for vs. foreach[^].
Of course, that's all based on 1.1. Things may have been shaken up and optimized in the CLR v2.
|
|
|
|
|
Thanks for the link. I will study that, and I will try to look with VS Express C++
at assembly code the JIT generated from C#-MSIL too.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
That's what i thougt (i used the c++ version: for(int i ...) but did not consider it to be much less efficient than the foreach loop. But i am greatfull for this solution !! Thanks
3.30 sec -> 1 second ;P jippie
|
|
|
|
|
I just started learning .Net and C#.
I come from C++ background.
When I declare a class vble
// class A is defined;
A a;
a = new A();
This object a is allocated in heap right? and its a reference type?
It seams all class variables are reference types. And it looks like a class pointer to me when I relate to C++.
Any good article?
thanks,
Misha
Evil traiumphs when good people sit quiet...
|
|
|
|
|
|
misha_grewal wrote: This object a is allocated in heap right? and its a reference type?
Correct.
It seams all class variables are reference types.
That's right. A class is always a reference type, and a struct is always a value type.
And it looks like a class pointer to me when I relate to C++.
A reference is implemented as a pointer, but it's easier to use in the code. You don't have to dereference a reference to get to the object, as you do with a pointer.
---
single minded; short sighted; long gone;
|
|
|
|
|
private void button1_Click(object sender, EventArgs e)
{
int n = 0;
textBox2.Visible = false;
textBox1.Visible = false;
while (n<100)
{
Thread.Sleep(100);
n++;
}
}
when I click the button, the textBox1 and textBox2 still visible! After 10sec, they are invisible,why?
NOTE:this code run in PPC, if run in WINDOWS, that two textBox will invisible after I click the button,is difference in PPC,why?
|
|
|
|
|
while (n<100)
{
Thread.Sleep(100);
n++;
}
You are making your UI sleep for 10 seconds -- once it gets out of the loop, the graphics object gets invalidated and the Paint handler gets to repaint your boxes (as invisible).
|
|
|
|
|
Hello,
You have to call:
textBox2.Visible = false;
textBox1.Visible = false;
Application.DoEvents();
bevore you start the Thread.Sleep
All the best,
Martin
|
|
|
|
|
How can i print an image using GDI+ in C# in avery dennison ap 4.4 printer. It is a ticket printer which accepts 3.9cm X 7.9 cm sheet?
|
|
|
|
|
Does anything in the System.Windows.Forms (such as the PrintDialog class) work?
If not, and you need to go outside the .NET framework for this, you can use P/Invoke[^] to call Win32 APIs.
|
|
|
|
|
I tried it came in reverse order with lot of space left on top. It should exactly match the ticket. So i need to adjust the printoutput same as ticket size. All these are readonly properties. I saw one PrintTicket class(in System.Printing namespace and in 2.0 all these are in System.Drawing.Printing namespace) in .Net framework 3.0. But there also same issue. print is coming in 3 tickets instead of one.
|
|
|
|
|
Hi guys,
I have tried numerous ways to export a report to PDF and I keep failing. Please help me, I am trying to export a crystal report from crystal report viewer to .pdf format automatically. Here is my code, it creates the report but it is not a .pdf and doesn't open in acrobat.
O_Report.Load(@"C:\Documents and Settings\sameer.patwa\My Documents\Project Files\POS\POSPymt\Invoice.rpt");
O_Report.Database.Tables[0].ApplyLogOnInfo(LogInfo);
pv.Value=SlsId;
pvs.Add(pv);
O_Report.DataDefinition.ParameterFields["Ad_Parameter1"].ApplyCurrentValues(pvs);
ExportOptions exp= new ExportOptions();
ExcelFormatOptions excelFormatOpts = new ExcelFormatOptions ();
DiskFileDestinationOptions dsk = new DiskFileDestinationOptions();
exp = O_Report.ExportOptions;
exp.ExportDestinationType = ExportDestinationType.DiskFile;
exp.ExportFormatType=ExportFormatType.PortableDocFormat;
dsk.DiskFileName = @"C:\" + SlsId + ".pdf";
exp.DestinationOptions = dsk;
O_Report.Export();
sasa
|
|
|
|