optimization

Is it more efficient to call the .net Garbage collector?

Due to the overhead of calling the garbage collector in the CLR, is it more efficient to leave it, or force to garbage collection when objects go out of scope? ...

Using return instead of else in Javascript

I am working on a project which requires some pretty intricate Javascript processing. This includes a lot of nested if-elses in quite a few places. I have generally taken care to optimise Javascript as much as possible by reading the other tips in SO, but I am wondering if the following two constructs would make any difference in terms o...

PostgreSQL: How to optimize my database for storing and querying a huge graph

I'm running PostgreSQL 8.3 on a 1.83 GHz Intel Core Duo Mac Mini with 1GB of RAM and Mac OS X 10.5.8. I have a stored a huge graph in my PostgreSQL database. It consists of 1.6 million nodes and 30 million edges. My database schema is like: CREATE TABLE nodes (id INTEGER PRIMARY KEY,title VARCHAR(256)); CREATE TABLE edges (id INTEGER,li...

Optimized method for calculating cosine distance in Python

I wrote a method to calculate the cosine distance between two arrays: def cosine_distance(a, b): if len(a) != len(b): return False numerator = 0 denoma = 0 denomb = 0 for i in range(len(a)): numerator += a[i]*b[i] denoma += abs(a[i])**2 denomb += abs(b[i])**2 result = 1 - numerator / (sqrt(den...

Optimize jquery code

$(textBox).focus( function() { $(spans).css({"background-position": "0 100%"}); }); $(textBox).blur( function() { $(spans).css({"background-position": "0 0"}); }); This is already short but it's either I am just too paranoid, or we could code this shorter by $(textBox).bind('focus blur', function() { *do toggle here* }); or ...

When was this clock bought?

The clock on the gym wall also shows the day name and the day of the month. This morning it showed Tuesday - 23. The day obviously rotates through a cycle of 7 - and showed "Tuesday" correctly. The day of the month, though, presumably rotates through a cycle of 31 and showed "23" incorrectly - today is the 1st December (ie. not the 31s...

Optimising DATEDIFF

I just want to check my logic there. Suppose I want to find all new products in the last 30 days. My current procedure is this: SELECT ProductName FROM ProductTable WHERE DATEDIFF( d, CreateDate, GETDATE() ) < 30 However, I understand that functions like DATEDIFF will not use the non-clustered index I have created on CreateDate. So t...

Write to two cells at same time excel vba

I need to write the same data into two different range of cells for a VBA application that I am writing. I could of course just loop through twice and write the data, but I was hoping to do it in one pass. This is an example of what I am doing (lot of complexity removed). Sub WriteData() WriteOutDivision "Division1",10 Writ...

How much should I add when resizing an array?

I'm having a contest with another student to make the fastest version of our homework assignment, and I'm not using an ArrayList for performance reasons (resizing the array myself cut the benchmark time from 56 seconds to 4), but I'm wondering how much I should resize the array every time I need to. Specifically the relevant parts of my ...

configuring e-tags

I am using Yslow as a simple speed benchmarking tool and I came across a really confusing concept. The E-tag So the main problem is : How do I configure E-tags? my grade in yslow says: There are 19 components with misconfigured ETags * http://thehotelinventory.com/media/js/jquery.min.js * http://thehotelinventory.com/media/js/jquery.c...

How I can optimize this slow query?

I want to know how I can optimize it, preferably without changes in tables structure. SELECT p.author_id member_id, m.members_display_name member_name, COUNT(p.pid) posts FROM forum_topics t STRAIGHT_JOIN forum_posts p STRAIGHT_JOIN forum_members m WHERE p.author_id != 0 AND p.author_id = m.member_id ...

When in optimization phase, would it be smart to pass all array by ref?

In PHP, When we get to the optimization phase of the application (I am speaking about an app that handles hundreds of thousands of users) would it be smart to pass all arrays (where I do not mind what happens to them after I pass them to the function) by ref? ...

SQL Query works quickly with 19 items in "IN" clause - much slower with 20. Why?

I have a query that includes this: ... AND Record.RecordID IN (1,2,3,10,11,12,13,16,17,18,26,27,28,557,31,32,33,36,37,93) AND ... The problem seems to be that if there are 20 items or more in that list, the query takes over 25 seconds to execute. If there are less than 20, it executes immediately. Any ideas on how to optimize? ...

How would you make this switch statement as fast as possible?

2009-12-04 UPDATE: For profiling results on a number of the suggestions posted here, see below! The Question Consider the following very harmless, very straightforward method, which uses a switch statement to return a defined enum value: public static MarketDataExchange GetMarketDataExchange(string ActivCode) { if (ActivCode == ...

sqlite: mirrorred ID-only table, worth it or not

I have a project where I need to load all ids of a table and have random access for different records afterward. This can be effectively done with loading results from SELECT rowid FROM... into an array and accessing actual data with SELECT * ... WHERE rowid = . This approach gives decent results for example for a 90M/70,000 records tab...

Different ways of adding to Dictionary

What is the difference in Dictionary.add(key, value) and Dictionary[key] = value? I've noticed that the last version does not throw an ArgumentException when inserting a duplicate key, but is there any reason to prefer the first version? Edit: Does anyone have an authoritative source of information about this? I've tried MSDN, but it i...

c# Optimized Select string creation loop

I'm looking to optimized this piece of code. It will process 15000 - 20000 lines. For now I have 9000 lines and it take 30 sec approx. I know string concatenation is slow but I don't know how to do it another way. // // Check if composite primary keys existe in database // ...

Turning off an index in Oracle

To optimize SELECT queries, I run them both with and without an index and measure the difference. I run a bunch of different similar queries and try to select different data to make sure that caching doesn't throw off the results. However, on very large tables, indexes take a really long time to create, and I have several different ide...

SQL optimization question (oracle)

Edit: Please answer one of the two answers I ask. I know there are other options that would be better in a different case. These other potential options (partitioning the table, running as one large delete statement w/o committing in batches, etc) are NOT options in my case due to things outside my control. I have several very large ...

Basic Java threading issue

Lets say I'm interacting with a system that has two incrementing counters which depend on each other (these counters will never decrement): int totalFoos; // barredFoos plus nonBarredFoos int barredFoos; I also have two methods: int getTotalFoos(); // Basically a network call to localhost int getBarredFoos(); // Basically a network call...