optimization

How expensive are JS function calls (compared to allocating memory for a variable)?

Given some JS code like that one here: for (var i = 0; i < document.getElementsByName('scale_select').length; i++) { document.getElementsByName('scale_select')[i].onclick = vSetScale; } Would the code be faster if we put the result of getElementsByName into a variable before the loop and then use the variable after that? I am...

How much speed-up from converting 3D maths to SSE or other SIMD?

I am using 3D maths in my application extensively. How much speed-up can I achieve by converting my vector/matrix library to SSE, AltiVec or a similar SIMD code? ...

Fastest way to see how many bytes are equal between fixed length arrays.

I have 2 arrays of 16 elements (chars) that I need to "compare" and see how many elements are equal between the two. This routine is going to be used millions of times (a usual run is about 60 or 70 million times), so I need it to be as fast as possible. I'm working on C++ (C++Builder 2007, for the record) Right now, I have a simple: ...

Why doesn't GCC optimize structs?

Systems demand that certain primitives be aligned to certain points within the memory (ints to bits that are multiples of 4, shorts to bits that are multiples of 2, etc.). Of course, these can be optimized to waste the least space in padding. my question is why doesn't GCC do this automatically? Is the more obvious heuristic (order va...

Lazy Function Definition in PHP - is it possible?

In JavaScript, you can use Lazy Function Definitions to optimize the 2nd - Nth call to a function by performing the expensive one-time operations only on the first call to the function. I'd like to do the same sort of thing in PHP 5, but redefining a function is not allowed, nor is overloading a function. Effectively what I'd like to d...

Should an index be optimised after incremental indexes in Lucene?

We run full re-indexes every 7 days (i.e. creating the index from scratch) on our Lucene index and incremental indexes every 2 hours or so. Our index has around 700,000 documents and a full index takes around 17 hours (which isn't a problem). When we do incremental indexes, we only index content that has changed in the past two hours, s...

Optimising a SELECT query that runs slow on Oracle which runs quickly on SQL Server

I'm trying to run the following SQL statement in Oracle, and it takes ages to run: SELECT orderID FROM tasks WHERE orderID NOT IN (SELECT DISTINCT orderID FROM tasks WHERE engineer1 IS NOT NULL AND engineer2 IS NOT NULL) If I run just the sub-part that is in the IN clause, that runs very quickly in Oracle, i.e. SELECT DISTINCT orde...

Slow treeview in C#

I have a legacy application that is written in C# and it displays a very complex treeview with 10 to 20 thousand elements. In the past I encountered a similar problem (but in C++) that i solved with the OWNERDATA capability offered by the Win32 API. Is there a similar mechanism in C#? EDIT: The plan is to optimize the creation time as...

How to reduce javax.faces.ViewState in JSF

What is the best way to reduce the size of the viewstate hidden field in JSF? I have noticed that my view state is approximately 40k this goes down to the client and back to the server on every request and response espically coming to the server this is a significant slowdown for the user. My Environment JSF 1.2, MyFaces, Tomcat, Toma...

What is the STL implementation with the lowest memory footprint?

I am working on a very large scale computing library that is using STL heavily. The library is being built using MSVC2003 and it is using its STL implementation. I am looking for an alternative STL implementation that would help the library lower its memory requirements and increase its performance. It is not possible to switch to a new...

Reducing memory footprint of large unfamiliar codebase.

Suppose you have a fairly large (~2.2 MLOC), fairly old (started more than 10 years ago) Windows desktop application in C/C++. About 10% of modules are external and don't have sources, only debug symbols. How would you go about reducing application's memory footprint in half? At least, what would you do to find out where memory is consu...

C# Compiler Incorrectly Optimizes Code

I have a ASP.NET application running on a remote web server and I just started getting this error: Method not found: 'Void System.Collections.Generic.ICollection`1..ctor()'. I disassembled the code in the DLL and it seems like the compiler is incorrectly optimizing the code. (Note that Set is a class that implements a set of unique o...

What are the advantages and disadvantages of GPGPU (general-purpose GPU) development?

I am wondering what is the key thing that helps you in GPGPU development and of course what is the constraints that you find unacceptable. Comes to mind for me: Key advantage: the raw power of these things Key constraint: the memory model What's your view? ...

To Update blindly or to Update Where?

I have a table that holds information about cities in a game, you can build one building each turn and this is recorded with the value "usedBuilding". Each turn I will run a script that alters usedBuilding to 0, the question is, which of the following two ways is faster and does it actually matter which way is used? UPDATE cities SET u...

How do you pre-size an array in Lua?

I've got a Lua program that seems to be slower than it ought to be. I suspect the issue is that I'm adding values to an associative array one at a time and the table has to allocate new memory each time. There did seem to be a table.setn function, but it fails under Lua 5.1.3: stdin:1: 'setn' is obsolete stack traceback: [C]...

Does an index help with < or > MySQL queries?

If I have a query like "DELETE FROM table WHERE datetime_field < '2008-01-01 00:00:00'", does having the 'datetime_field' column indexed help? i.e. is the index only useful when using equality (or inequality) testing, or is it useful when doing an ordered comparison as well? (Suggestions for better executing this query, without recreat...

Is there a good tool for MySQL that will help me optimise my queries and index settings?

I use MySQL in a fairly complex web site (PHP driven). Ideally, there would be a tool I could use that would help me test the SQL queries I am using and suggest better table indexes that will improve performance and avoid table scans. Failing that, something that will tell me exactly what each query is up to, so I can perform the optim...

PHP Optimization Tips

I'm looking for PHP Optimization tips. Coding practices and other methodologies which will make my PHP execute faster. One tip per answer, please, and include why it makes the code faster! This is not about HTML or Javascript execution, but purely server side PHP execution. ...

Should Python import statements always be at the top of a module?

PEP 08 states: Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants. However if the class/method/function that I am importing is only used in rare cases, surely it is more efficient to do the import when it is needed? Isn't this: class SomeClass(obje...

How do I write a generic memoize function?

I'm writing a function to find triangle numbers and the natural way to write it is recursively: function triangle (x) if x == 0 then return 0 end return x+triangle(x-1) end But attempting to calculate the first 100,000 triangle numbers fails with a stack overflow after a while. This is an ideal function to memoize, but I want a...