optimization

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...

Does foreach always create a copy on a none reference in PHP?

I'm wondering if PHP has this optimization built in. Normally when you call foreach without using a reference it copies the passed array and operates on it. What happens if the reference count to that array is only 1? Say for example if getData returns some array of data. foreach(getData() as $data) echo $data; Since the array ...

Is there a memory efficient replacement of java.lang.String?

After reading this old article measuring the memory consumption of several object types, I was amazed to see how much memory Strings use in Java: length: 0, {class java.lang.String} size = 40 bytes length: 7, {class java.lang.String} size = 56 bytes While the article has some tips to minimize this, I did not find them entirely satisfy...

How to use Explain Plan to optimize queries?

I have been tasked to optimize some sql queries at work. Everything I have found points to using Explain Plan to identify problem areas. The problem I can not find out exactly what explain plan is telling me. You get Cost, Cardinality, and bytes. What do this indicate, and how should I be using this as a guide. Are low numbers better? ...

Do modern compilers optimize the x * 2 operation to x << 1?

Does the C++ compiler optimize this operation? I would love to believe that yes. ...

Tickmark algorithm for a graph axis

I'm looking for an algorithm that places tick marks on an axis, given a range to display, a width to display it in, and a function to measure a string width for a tick mark. For example, given that I need to display between 1e-6 and 5e-6 and a width to display in pixels, the algorithm would determine that I should put tickmarks (for exa...

When have you used dynamic programming in the field?

When have you ever directly applied the concepts of dynamic programming to solve a problem in the field? It's sometimes not evident how it can be applied when using it to solve a made-up instance of the knapsack problem. ...

Fastest way to convert string to integer in PHP

Using PHP, what's the fastest way to convert a string like this: "123" to an integer? Why is that particular method the fastest? What happens if it gets unexpected input, such as "hello" or an array? ...

C# .NET: How to check if we're running on battery?

i want to be a good developer citizen, pay my taxes, and disable things if we're running over Remote Desktop, or running on battery. If we're running over remote desktop (or equivalently in a Terminal server session), we must disable animations and double-buffering. You can check this with: /// <summary> /// Indicates if we're running ...

When should I optimize?

They say "Premature optimization is the root of all evil".. so what's the best point in time for optimization? ...

Profiling VBA code for microsoft word

I have some legacy code that uses VBA to parse a word document and build some XML output; Needless to say it runs like a dog but I was interested in profiling it to see where it's breaking down and maybe if there are some options to make it faster. I don't want to try anything until I can start measuring my results so profiling is a m...

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...

Best resource for learning about prefetching a buffer in C on Intel/AMD 64 bit

I am interested in mastering prefetch-related functions such as _mm_prefetch(...) so when I perform operations that loop over arrays, the memory bandwidth is fully utilized. What are the best resources for learning about this? I am doing this work in C using GCC 4 series on an intel linux platform. ...

When is not a good time to use python generators?

This is rather the inverse of What can you use Python generator functions for?: python generators, generator expressions, and the itertools module are some of my favorite features of python these days. They're especially useful when setting up chains of operations to perform on a big pile of data--I often use them when processing DSV fil...

Is there a way to enforce function inlining in c#?

As far as I know there's no way to hint the c# compiler to inline a particular function and I guess it's like that by design. I also think that not letting the programmer to specify what to inline and what not is generally a good idea, as it would imply that you think you're smarter than the JIT compiler (my respects to those who actual...

How can I optimize this Google App Engine code?

I'm relatively new to the python world, but this seems very straight forward. Google is yelling at me that this code needs to be optimized: class AddLinks(webapp.RequestHandler): def post(self): # Hash the textarea input to generate pseudo-unique value hash = md5.new(self.request.get('links')).hexdigest() ...

Query optimization techniques?

How to optimize queries which are already written? ...

How do I gzip webpage output with Rails?

What is the best plugin for Rails that gzips my webpage output? I found this plugin but it's out of date and I am not sure if I should use it. -- edit -- I am running my server in a hosted environment and mod_deflate is not an option . -- edit 2 -- The company I am hosting with has stated they will not install mod_deflate as state...

Fastest way to iterate over a stack in c#

I feel that using GetEnumerator() and casting IEnumerator.Current is expensive. Any better suggestions? I'm open to using a different data structure if it offers similiar capabilities with better performance. After thought: Would a generic stack be a better idea so that the cast isn't necessary? ...

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...