micro-optimization

Speed of CSS

This is just a question to help me understand CSS rendering better. Lets say we have a million lines of this. <div class="first"> <div class="second"> <span class="third">Hello World</span> </div> </div> Which would be the fastest way to change the font of Hello World to red? .third { color: red; } div.third { color:...

C++ Optimization on negative integers

Lets say we have a negative integer say int a; is there a faster implementation of -a? Do I have to do some bitwise operation on this? ...

What's the most efficient way to make bitwise operations in a C array

I have a C array like: char byte_array[10]; And another one that acts as a mask: char byte_mask[10]; I would like to do get another array that is the result from the first one plus the second one using a bitwise operation, on each byte. What's the most efficient way to do this? thanks for your answers. ...

Optimize Binary Search Algorithm

In a binary search, we have two comparisons one for greater than and other for less than, otherwise its the mid value. How would you optimize so that we need to check only once? bool binSearch(int array[], int key, int left, int right) { mid = left + (right-left)/2; if (key < array[mid]) return binSearch(array, key, lef...

Does rearranging a conditional evaluation speed up a loop?

Bit of a weird one: I was told a while ago by a friend that rearranging this example for loop from : for(int i = 0; i < constant; ++i) { // code... } to: for(int i = 0; constant > i; ++i) { // code... } would slightly increase performance in C++. I don't see how comparing a constant value to a variable is faster than vice-...

funny enough,this is probably a stack overflow problem

Hello all The following procedure (explanation follows) works fine for really small lists, but when the list contains a larger number of items (1/2 million) the application enters "not responding" state,and it takes about 2.5 minutes to finish (very bad time). I might add the application needs to process lists of 100 million items at le...

array_push() vs. $array[] = .... Which is fastest?

Hi all I need to add values received from MySQL into an array [PHP], here is what I've got: $players = array(); while ($homePlayerRow = mysql_fetch_array($homePlayerResult)) { $players[] = $homePlayerRow['player_id']; } Is this the only way of doing it? Also, is the following faster/better? $players = array(); while ($homePlay...

Does the order of cases matter in PHP switch statements?

In PHP switch statements, does placing more common cases near the top improve performance? For example, say the following function is called 1,000 times: <?php function foo_user ($op) { switch ($op) { case 'after_update': //Some Stuff case 'login': //Some other Stuff } } If in 990 of the 1,000 of the tim...

C coding practices for performance or code size - beyond what a compiler does

I'm looking to see what can a programmer do in C, that can determine the performance and/or the size of the generated object file. For e.g, 1. Declaring simple get/set functions as inline may increase performance (at the cost of a larger footprint) 2. For loops that do not use the value of the loop variable itself, count down to zero in...

Fast Euclidean division in C

I am interested in getting the remainder of the Euclidean division, that is, for a pair of integers (i, n), find r such as: i = k * n + r, 0 <= r < |k| the simple solution is: int euc(int i, int n) { int r; r = i % n; if ( r < 0) { r += n; } return r; } But since I need to execute this tens of million o...

Does using xor reg, reg give advantage over mov reg, 0?

There're two well-known ways to set an integer register to zero value on x86. Either mov reg, 0 or xor reg, reg There's an opinion that the second variant is better since the value 0 is not stored in the code and that saves several bytes of produced machine code. This is definitely good - less instruction cache is used and this ca...

Smart JVM and JIT Micro-Optimizations

Over time, Sun's JVM and JIT have gotten pretty smart. Things that used to be common knowledge as being a necessary micro-optimization are no longer needed, because it gets taken care of for you. For example, it used to be the case that you should mark all possible classes as final, so the JVM inlines as much code as possible. However...

PHP Function execution cost table

There is a reference table that shows the execution cost of every php function? I know that the time execution is boundet to many factors and is impossible to determinate an unique value, but im looking for a 'ideological' table. For example, is_dir() = cost 3 is_file() = cost 2 (take it JUST as an example ;) If i dont remember bad...

What is the optimal way for reading the contents of a webpage into a string in Java?

I have the following Java code to fetch the entire contents of an HTML page at a given URL. Can this be done in a more efficient way? Any improvements are welcome. public static String getHTML(final String url) throws IOException { if (url == null || url.length() == 0) { throw new IllegalArgumentException("url cannot be null or empty...

Optimize CSS: Narrow Definition (#mytable tbody span.myclass) better?

Hello, I wondered whether or not a 'narrow' definition such as #mytable tbody span.myclass { color: #ffffff; } is better/faster to parse than just .myclass { color: #ffffff; } I read somewhere that narrow definitions supposedly actually have some kind of adversery effect on CSS speed, but I can't remember where a...

C pointers vs direct member access for structs

Say I have a struct like the following ... typedef struct { int WheelCount; double MaxSpeed; } Vehicle; ... and I have a global variable of this type (I'm well aware of the pitfalls of globals, this is for an embedded system, which I didn't design, and for which they're an unfortunate but necessary evil.) Is it faster to access t...

What is faster in Python, "while" or "for xrange"

We can do numeric iteration like: for i in xrange(10): print i, and in C-style: i = 0 while i < 10: print i, i = i + 1 Yes, I know, the first one is less error-prone, more pythonic but is it fast enough as C-style version? PS. I'm from C++ planet and pretty new on Python one. ...

Java, Most Expensive Statements?

What are the most expensive (both in terms of bytecode and cpu cycles) statements in the Java Programming language? ...

Placing index columns on the left of a mysql WHERE statement?

I was curious since i read it in a doc. Does writing select * from CONTACTS where id = ‘098’ and name like ‘Tom%’; speed up the query as oppose to select * from CONTACTS where name like ‘Tom%’ and id = ‘098’; The first has an indexed column on the left side. Does it actually speed things up or is it superstition? Using php and my...

Optimizing Java code

How can I optimize this code ? I made IPFilter and I need to optimize it. package com.ipfilter; import java.util.HashMap; import java.util.Map; /** * IPFilter * * Loads given IP addresses to memory, so you can easily check if ip addres has been blocked */ public class IPFilter { private Map<Integer, IPFilter> ...