running-time

Do sealed classes really offer performance Benefits?

I have come across a lot of optimization tips which say that you should mark your classes as sealed to get extra performance benefits. I ran some tests to check the performance differential and found none. Am I doing something wrong? Am I missing the case where sealed classes will give better results? Has anyone run tests and seen a di...

Big O, how do you calculate/approximate it?

Most people with a degree in CS will certainly know what Big O stands for. It helps us to measure how (in)efficient an algorithm really is and if you know in what category the problem you are trying to solve lays in you can figure out if it is still possible to squeeze out that little extra performance.* But I'm curious, how do you calc...

Performance Considerations for throwing Exceptions

I have come across the following type of code many a times, and I wonder if this is a good practice (from Performance perspective) or not: try { ... // some code } catch (Exception ex) { ... // Do something throw new CustomException(ex); } Basically, what the coder is doing is that they are encompassing the exception in a ...

Unit test execution speed (how many tests per second?)

What kind of execution rate do you aim for with your unit tests (# test per second)? How long is too long for an individual unit test? I'd be interested in knowing if people have any specific thresholds for determining whether their tests are too slow, or is it just when the friction of a long running test suite gets the better of you?...

Which compiles to faster code: "n * 3" or "n+(n*2)"?

Which compiles to faster code: "ans = n * 3" or "ans = n+(n*2)"? Assuming that n is either an int or a long, and it is is running on a modern Win32 Intel box. Would this be different if there was some dereferencing involved, that is, which of these would be faster? long a; long *pn; long ans; ... *pn = some_number; ans = ...

How do you find Leapyear in VBA?

What is a good implementation of a IsLeapYear function in VBA? Edit: I ran the if-then and the DateSerial implementation with iterations wrapped in a timer, and the DateSerial was quicker on the average by 1-2 ms (5 runs of 300 iterations, with 1 average cell worksheet formula also working). ...

Are doubles faster than floats in c#?

I'm writing an application which reads large arrays of floats and performs some simple operations with them. I'm using floats because I thought it'd be faster than doubles, but after doing some research I've found that there's some confusion about this topic. Can anyone elaborate on this? Thanks. ...

How do you test running time of VBA code?

Is there code in VBA I can wrap a function with that will let me know the time it took to run, so that I can compare the different running times of functions? ...

How do I find the running time given algorithm speed and computer speed?

I'm currently working through an assignment that deals with Big-O and running times. I have this one question presented to me that seems to be very easy, but I'm not sure if I'm doing it correctly. The rest of the problems have been quite difficult, and I feel like I'm overlooking something here. First, you have these things: Algorithm ...

How to speed up WPF programs?

I love programming with and for Windows Presentation Framework. Mostly I write browser-like apps using WPF and XAML. But what really annoys me is the slowness of WPF. A simple page with only a few controls loads fast enough, but as soon as a page is a teeny weeny bit more complex, like containing a lot of data entry fields, one or two t...

Best way to reverse a string in C# 2.0

I've just had to write a string reverse function in C# 2.0 (i.e. LINQ not available) and came up with this: public string Reverse(string text) { char[] cArray = text.ToCharArray(); string reverse = String.Empty; for (int i = cArray.Length - 1; i > -1; i--) { reverse += cArray[i]; } return reverse; } Per...

What is an Efficient algorithm to find Area of Overlapping Rectangles

My situation Input: a set of rectangles each rect is comprised of 4 doubles like this: (x0,y0,x1,y1) they are not "rotated" at any angle, all they are "normal" rectangles that go "up/down" and "left/right" with respect to the screen they are randomly placed - they may be touching at the edges, overlapping , or not have any contact I w...

How to find the kth largest element in an unsorted array of length n in O(n)?

I believe there's a way to find the kth largest element in an unsorted array of length n in O(n). Or perhaps it's "expected" O(n) or something. How can we do this? Cheers! p.s. this is not for homework. ...

fast geometric proximity predicate

I have 3 points (A, B and X) and a distance (d). I need to make a function that tests if point X is closer than distance d to any point on the line segment AB. The question is firstly, is my solution correct and then to come up with a better (faster) solution. My first pass is as follows AX = X-A BX = X-B AB = A-B // closer than...

String operation optimisation in C#

The following C# code takes 5 minutes to run: int i = 1; string fraction = ""; while (fraction.Length < 1000000) { fraction += i.ToString(); i++; } "Optimising it" like this causes it to run in 1.5 seconds: int i = 1; string fraction = ""; while (fraction.Length < 1000000) { // concatenating strings is much faster for sma...

Performance metrics on specific routines: any best practices?

I'd like to gather metrics on specific routines of my code to see where I can best optimize. Let's take a simple example and say that I have a "Class" database with multiple "Students." Let's say the current code calls the database for every student instead of grabbing them all at once in a batch. I'd like to see how long each trip to th...

C++: delete vs. free and performance

Consider: char *p=NULL; free(p) // or delete p; What will happen if i use free and delete on p? If a program taking more time for execution, suppose 10 minutes, is there any way to reduce its running time to 5 minutes? ...

Back to basics; for-loops, arrays/vectors/lists, and optimization

I was working on some code recently and came across a method that had 3 for-loops that worked on 2 different arrays. Basically, what was happening was a foreach loop would walk through a vector and convert a DateTime from an object, and then another foreach loop would convert a long value from an object. Each of these loops would store...

Math optimization in C#

I've been profiling an application all day long and, having optimized a couple bits of code, I'm left with this on my todo list. It's the activation function for a neural network, which gets called over a 100 million times. According to dotTrace, it amounts to about 60% of the overall function time. How would you optimize this? pub...

Float vs Double Performance

I did some timing tests and also read some articles like this one (last comment),and it looks like in Release build, float and double values take the same amount of processing time. How is this possible? When float is less precise and smaller compared to double values, how can the CLR get doubles into the same processing time? Edit: ...