IMO do the second tip first (MAJOR speed increase, between 5 and 100 times), then do the first one (speed increase depends on the processor, best-case scenario is 1x extra increase for each processor core [such as 4x for a 4-core processor]). After those two major optimizations (Use .NET 4.0 Beta's Parallel.For method for the first tip), profile the code (NProf is a good sampling profiler, also Visual Studio 2010 Beta 2 [free to use until June] comes with an excellent profiler. If you have the money, use ANTS, as I have heard only good things about it) and fix the bottlenecks (those areas that take the most time).
StarBP
Posts
-
Fast execution -
beginner question: foreach .. else?//Print header text
Console.WriteLine("The products that were found:");//loop through products in selection
foreach (var aProduct in myProductSelection)
{
Console.WriteLine("Found product: " + aProduct.Code);
}
else
{
Console.WriteLine("No products were found!");
}Try this:
//Print header text
Console.WriteLine("The products that were found:");//Initialize tracker variable
int productsFound = 0;//loop through products in selection
foreach (var aProduct in myProductSelection)
{
Console.WriteLine("Found product: " + aProduct.Code);
productsFound++;
}if (productsFound == 0)
Console.WriteLine("No products were found!"); -
How to search for a specific string in a file?100 bytes is just fine. Search once with a stride of 100 bytes starting at zero-indexed byte 0, then search with a stride of 100 bytes starting at zero-indexed byte 50. This works with strings up to 50 bytes in length.
-
Zip Folders and Files -
How to search for a specific string in a file?Test it twice. First test it like usual, then test it again with the divisions offset by exactly half the batch size. As long as each batch is at least twice the size of the search string, you will be guaranteed to find the string in at least one of the two searches. More specifically, the offset MUST be less than or equal to Half the batch size AND greater than or equal to the size of the string being searched for.
-
Please help as i cant remember how to do this.....There may be something in the GMP Bignum Library that can help you. Try searching the Internet for GMP C# bindings.
-
GDI+ P/InvokeHere is the complete source code of the program. It may only be used under the provisions of the GPL. This is not Roger Alsing's original work; it has been very highly optimized. Further optimization of other sections is almost useless without optimization of the renderer. I tried Direct3D, which was very good, but it had WAY too many bugs to even attempt to continue on. The Render method is in the Renderer.cs file and is called by FitnessCalculator.cs, which is in turn called by MainForm.cs. The DnaPolygon.cs and DnaDrawing.cs files are used extensively. Download Source Code The two lines of the Render method that I mentioned in the original post together take more than 70% of the CPU time of the program.
-
GDI+ P/InvokeThanks, but how do I apply this using C# code? Oh, and what you showed me is normal GDI. System.Graphics uses GDI+. Is this any problem? (I have heard normal GDI is faster, though; is this true?). Here is a slightly modified version of the context within which the code is used:
if (!isPerformanceTest) { Renderer.Render(newDrawing /\*The real method calls for this; this is an object that contains the Point\[\], Color, and SolidBrush\*/, g)); } bitmapData = bitmap.LockBits( new Rectangle(0, 0, (int)(Tools.MaxWidth / reduction), (int)(Tools.MaxHeight / reduction)), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
// After this, some highly optimized math is done to compare each pixel of the drawing to the input picture
I would like some code if at all possible.
-
GDI+ P/InvokeHello. I have searched for a way to P/Invoke the Graphics.FillPolygon(Brush, Point[]) method. I saw that it is much faster to do so (likely because it bypasses error checking that is unneeded in a well-made program). However, I have not found any examples of how to do so (even .NET Reflector is of no help, only referring me to a P/Invoke that uses a private native field). Please help me translate this code into a P/Invoke. The FillPolygon method is the most important by far, as it is called 10 times more often in the real code as the Clear method is. This Render method takes up an average of 75% of the time in the code, as profiled by NProf (a specially modified version that samples 5x as often as usual was used).
public static void Render(Color color, SolidBrush brush, Point[] points, Graphics graphics)
{
graphics.Clear(color);
graphics.FillPolygon(brush, points);
} -
[Message Deleted][Message Deleted]
-
Polygon Rendering Performance OptimizationOn his blog, Roger Alsing describes a very interesting program. I wonder if it can be improved....? Here you can find a file with Dan Bystrom’s improvements [to the EvoLisa program], plus an additional improvement (using uints instead of doubles when the fitness drops below 3/4 of uint.MaxValue). I added an additional feature which could be implemented. It allows an interlaced subset of the pixels to be tested for fitness. I also included an open-source C program I found on the Internet that generates fast random numbers, but that would not help improve the program much speed-wise due to other bottlenecks in the system, so I did not implement it. Right now the renderer seems to be the major bottleneck in the code. You may want to try using System.Windows.Shapes.Polygon instead of System.Graphics for the polygons, since then each polygon could be rerendered at will individually. Right now, rendering is the major bottleneck in the code after the updates are applied (Around 350 renders occur per second on my computer, whereas 3,500 mutates or 1,750 fitness evaluations can occur in the same time period [I tested that by commenting out code temporarily and counting to ten while the algorithm was running. The margin of error was around 10%, which is negligible in this case]). By the way, I think that using uints instead of doubles was the 60% improvement that Dan was talking about, although the improvement would be more along the lines of 3X if the Renderer was faster. The question is, how can I implement this? (I am a high-school student who has recently begun programming, and I consider myself to be a novice-intermediate programmer). Could anyone optimize that code with all of this in mind? The key is to use System.Windows.Shapes.Polygon so only one polygon would usually need to be rendered per generation (maybe two or three, but certainly not all of them as is required now). Before you ask, NO, THIS IS NOT A HOMEWORK ASSIGNMENT!!!
-
Trying to create a for loop that gives all Unique combinations without using recursive process.using System;
using System.IO;namespace Schedule
{
class Program
{
static void Main(string[] args)
{
// Sample only.
GetThreeSchedules("schedule3.txt", 7, 6, 10);
GetFiveSchedules("schedule5.txt", 2, 3, 4, 1, 6);
}
public static void GetThreeSchedules(string filename, int one, int two, int three)
{
for (int a = 1; a <= one; a++)
{
for (int b = 1; b <= two; b++)
{
for (int c = 1; c <= three; c++)
{
File.AppendAllText(filename, "P1 S" + a + " P2 S" + b + " P3 S" + c + "\r\n");
}
}
}
}public static void GetFiveSchedules(string filename, int one, int two, int three, int four, int five) { for (int a = 1; a <= one; a++) { for (int b = 1; b <= two; b++) { for (int c = 1; c <= three; c++) { for (int d = 1; d <= four; d++) { for (int e = 1; e <= five; e++) { File.AppendAllText(filename, "P1 S" + a + " P2 S" + b + " P3 S" + c + " P4 S" + d + " P5 S" + e + "\\r\\n"); } } } } } } }
}
Here is some code. I do not know how to generalize it further, though.
-
Solve this......First of all, the DLL Delete code never gets called. However, this program should output "+~~~" and wait for input. The "&int32" is apparently a little-used escape character. Or maybe it outputs "729710206" :-) (No, I didn't run the code).
-
Polygon Rendering Performance OptimizationOn his blog, Roger Alsing describes a very interesting program. I wonder if it can be improved....? Here you can find a file with Dan Bystrom’s improvements [to the EvoLisa program], plus an additional improvement (using uints instead of doubles when the fitness drops below 3/4 of uint.MaxValue). I added an additional feature which could be implemented. It allows an interlaced subset of the pixels to be tested for fitness. I also included an open-source C program I found on the Internet that generates fast random numbers, but that would not help improve the program much speed-wise due to other bottlenecks in the system, so I did not implement it. Right now the renderer seems to be the major bottleneck in the code. You may want to try using System.Windows.Shapes.Polygon instead of System.Graphics for the polygons, since then each polygon could be rerendered at will individually. Right now, rendering is the major bottleneck in the code after the updates are applied (Around 350 renders occur per second on my computer, whereas 3,500 mutates or 1,750 fitness evaluations can occur in the same time period [I tested that by commenting out code temporarily and counting to ten while the algorithm was running. The margin of error was around 10%, which is negligible in this case]). By the way, I think that using uints instead of doubles was the 60% improvement that Dan was talking about, although the improvement would be more along the lines of 3X if the Renderer was faster. The question is, how can I implement this? (I am a high-school student who has recently begun programming, and I consider myself to be a novice-intermediate programmer). Could anyone optimize that code with all of this in mind? The key is to use System.Windows.Shapes.Polygon so only one polygon would usually need to be rendered per generation (maybe two or three, but certainly not all of them as is required now). Before you ask, NO, THIS IS NOT A HOMEWORK ASSIGNMENT!!!
modified on Wednesday, May 6, 2009 4:45 PM