optimization

volatile and double confusion

int x = 2; volatile int y = 2; const int z = x/y; int main(){ int x = 2 + 3; double d = 7 / 3; } I have three questions here: Firstly, can the compiler calculate the value of the 'z' at compile time to be 1 in this case? Secondly, I observed that the compiler does not generate assembly instructions for adding 2 and 3 to in...

can this JavaScript function be refactored?

I need to use prototype JavaScript library on a project and wanted to add tabbed box to HTML. The click handler has a simple task - set selected on parent <li> element and show linked DIV id element (rel tag on <li> has element id name) <div class="tabInterface"> <ul class="tabSelector"> <li class="selected" rel="searchLast"><...

single big files for PHP or lots of small ones?

I have a server which I try to optimize for speed (php/mysql). From a speed standpoint, is it better to keep a big file with all my functions in it so as to have only one file to include? Or is it better to have many, small, task-optimized includes that will include depending on the job at hand (that solution produces 4/5 even 6 files to...

Is this problem NP-hard?

I'm trying to come up with a reasonable algorithm for this problem: Let's say you have a bunch of balls. Each ball has at least one color, but can also be multicolored. Each ball also has a number on it. There are also a bunch of boxes which are each only one color. The goal is to maximize the sum of the numbers on the balls in the ...

Reproducing images with primitive shapes. (Graphics optimization problem)

Based on this original idea, that many of you have probably seen before: http://rogeralsing.com/2008/12/07/genetic-programming-evolution-of-mona-lisa/ I wanted to try taking a different approach: You have a target image. Let's say you can add one triangle at a time. There exists some triangle (or triangles in case of a tie) that maxi...

Efficient Method for Calculating the Probability of a Set of Outcomes?

Let's say I'm playing 10 different games. For each game, I know the probability of winning, the probability of tying, and the probability of losing (each game has different probabilities). From these values, I can calculate the probability of winning X games, the probability of losing X games, and the probability of tying X games (for ...

javascript constructs to avoid?

I have been writing a JS algorithm. Its blazing fast in chrome and dog slow in FF. In the chrome profiler, I spend <10% in a method, in FF the same method is 30% of the execution time. Are there javascript constructs to avoid because they are really slow in one browser or another? One thing I have noticed is that things like simple v...

Complicated code for obvious operations

Hi, Sometimes, mainly for optimization purposes, very simple operations are implemented as complicated and clumsy code. One example is this integer initialization function: void assign( int* arg ) { __asm__ __volatile__ ( "mov %%eax, %0" : "=m" (*arg)); } Then: int a; assign ( &a ); But actually I don't understand why is it w...

Binary Heap vs (new) B-Heap: Should it be implemented in the CLR/.NET, and where?

The following article discusses an alternative heap structure that takes into consideration that most servers are virtualized and therefore most memory is paged to disk. http://queue.acm.org/detail.cfm?id=1814327 Can (or should) a .NET developer implement a B-Heap data structure so that parent-child relationships are maintained within...

Practical Queuing Theory

I want to learn enough simple/practical queuing theory to model the behavior of a standard web application stack: Load balancer with multiple application server backends. Given a simple traffic pattern extracted from a tool like NewRelic showing percentage of traffic to a given part of an application and average response time for that...

Why does this divide-by-zero error only occur in optimized code?

I just found a bug that, strangely, occurred only when optimization was turned on (g++ -O2). It was an Arithmetic exception in the following code, when interval was set to zero (from a command line argument): for(int i = 0; i < n; ++i) { if((i + 1) % interval == 0) { // exception here DoSomething(); } } It's obvious that modul...

LINQ Query on ConcurrentDictionary of 19710 items slow

Hi All I have the following method: /// <summary> /// Calculates the last trade date. /// </summary> /// <param name="tradesDictionary">The trades dictionary.</param> /// <returns>The last trade date.</returns> public DateTime CalculateLastTradeDate(ConcurrentDictionary<Guid, TradeRecord> tradesDictionary) {...

Most efficient way - PHP/MYSQL requests - Real time news system

Hi there, I've got a quick performance question for my real time news system. This is a community news system, where everybody can post his news, and vote for it, or vote for other news. Every 20secs in jQuery I search in the DB to refresh the 20 last news/votes. But at the moment, I extract every 20sec the last 20 questions, even if ...

ASP.Net MVC - Images taking a long time to load

I have tested my pages in Firefox & IE and looking at Firebug in Firefox for some reason some images are taking a long time to load. They are not very big in comparison to the ones which are loading quicker. Attached is a screenshot of Firebug. I especially notice it in IE with the progress bar at the bottom of the page, it just sits ...

gcc optimizations cause app to fail

I'm having a real strange problem using GCC for ARM with the optimizations turned on. Compiling my C++ application without the optimizations produces an executable that at runtime outputs the expected results. As soon as I turn on the optimizations - that is -O1 - my application fails to produce the expected results. I tried for a cou...

Optimizing a cycle sort implementation

Cycle sort is an in-place sort based on the idea that the permutation you're sorting can be factored into cycles. If you rotate each cycle one position, the array will be sorted. This can easily be coded so that the number of writes to the array is the theoretical minimum needed for any in-place sort (which is nice for huge data sets on,...

Using hardware timer in C

Hi guys, Okay, so I've got some C code to perform a mathematical operation which could, pretty much, take any length of time (depending on the operands supplied to it, of course). I was wondering if there is a way to register some kind of method which will be called every n seconds which can analyse the state of the operation, i.e. what...

Using mysql COUNT(*)

Why is COUNT(*) so much quicker than COUNT(field), Even when field is an index? I am using MySQL ...

How to optimize this Nhibernate Query (835ms)

I have this query var temp = from x in ActiveRecordLinq.AsQueryable<Circuits>() where x.User_Created == false orderby x.Description select x; From NHibernate Profiler Query duration -Database only: 7ms -Total: 835ms The query generated: SELECT this_.Circuit...

How can I replicate something similar to querySelector in browsers like IE 7 and 6?

I've messed around with trying to detect how complex the query is (like if it's just an ID selector, it goes through getElementById instead and such) but this is clearly no way to do complex CSS queries and will probably fail on a certain few selectors. So my question is to anyone who's done something similar, how did you end up replica...