efficiency

What's the efficiency and quality of this shuffling algorithm?

This recent question about sorting randomly using C# got me thinking about the way I've sometimes shuffled my arrays in Perl. @shuffled = sort { rand() <=> rand() } @array; The proposed solution in the mentioned question is Fisher-Yates shuffle, which works in a linear time. The question is: how efficient is my snippet and is such sh...

Efficient Javascript String Replacement

Hey there, I've got a block of HTML that I'm going to be using repeatedly (at various times during a users visit, not at once). I think that the best way to accomplish this is to create an HTML div, hide it, and when needed take its innerHTML and do a replace() on several keywords. As an example HTML block... <div id='sample'> <h4>%...

Efficient maths algorithm to calculate intersections

For a game I am developing I need an algorithm that can calculate intersections. I have solved the problem, but the way I have done it is really nasty and I am hoping someone here might have a more elegant solution. A pair of points represent the end points of a line drawn between them. Given two pairs of points, do the drawn lines inte...

How can I make this Ruby on Rails page more efficient?

I'm building a site where users can track their collection of figures for Dungeons & Dragons (www.ddmdb.com). The models/relationships involved in this funcitonality are the following: User: id login (username) a bunch of other fields Miniature: id name number (# in the set, not count) release_id (foreign key) a bunch of other fi...

Check the signature on large data sets efficiently using JCA

I have to verify the signature on a file that may be as large as 2Gb, and I want to do so in a way that is as memory-efficient as possible. For various reasons, the file will already be loaded completely into memory, and is accessed using an InputStream by the application. I would like to verify the signature using the stream interface...

Time Calendar Data Structure

We are looking at updating (rewriting) our system which stores information about when people can reserve rooms etc. during the day. Right now we store the start and time and the date the room is available in one table, and in another we store the individual appointment times. On the surface it seemed like a logical idea to store the ...

Convert persisted ADO 2.8 COM recordset to ADO.Net DataSet

Hi All-- I have a VB6 application that I am converting to .net. I am doing this in phases so clients will have both VB6 and .net applications at the same. Part of the application caches ADO 2.8 COM recordsets to a table in SQL Server and retrieves them as needed. The .net application uses that same persisted recordsets. I have c# code...

Postgresql.conf variables

Hi again, I have been playing around with the postgresql.conf file for a couple days now. I was wondering what variables you guys like customizing and why? Here is a sample of the file: # - TCP Keepalives - # see "man 7 tcp" for details #tcp_keepalives_idle = 0 # TCP_KEEPIDLE, in seconds; # 0 selects the system default #tc...

How can I make this Processing.org sketch more efficient?

I have a simple sketch (in Processing), basically a bunch of dots wander around, if they come into contact with each other they fight (each has a strength value, increased each time they win, if it's equal the winner is randomly chosen) It works well with about 5000 12-pixel "zombies" (there's a slight slowdown for a half a second, whil...

Energy efficient application development

Are there any tools and techniques to build more energy efficient applications? Any articles, rules or best practices for green programming? ...

I have ported an Excel financial calculator to javascript. Javascript runs much slower than Excel. Help!

I've ported an Excel retirement calculator into javascript. There are 35 worksheets in the original excel containing many recursive calculations, all of which I've converted to JS. JS is running slower (1-2 seconds compared to Excel's instantaneous). I am caching the resursive calculations already to speed things up and prevent stack ove...

How do cursors work in Python's DB-API?

Hi again, I have been using python with RDBMS' (MySQL and PostgreSQL), and I have noticed that I really do not understand how to use a cursor. Usually, one have his script connect to the DB via a client DB-API (like psycopg2 or MySQLdb): connection = psycopg2.connect(host='otherhost', etc) And then one creates a cursor: cursor = co...

Writing efficient .Net/SQL Server code

Let me preface by saying I am not knocking .Net (it helps me earn my living)... I recently heard of a site were the developer/owner bragged that he had minimal hardware to support his site (plentyoffish.com). This site is listed as being in the top 50 in terms of traffic on Alexa. I am struggling to wrap my head around how something li...

MySQL: Most efficient way to count records added today

This is the MySQL table structure that I have: itemID (BIGINT) userID (BIGINT) wasAdded (DATETIME) I need to get the number of items introduced in the current day, by the X userID What would be the most efficient way to do this? Thanks ...

Extracting Album art from MP3 files using TagLib - Is there a better way write this code?

I'm using Visual Basic 9 (VS2008) and TagLib. The following code extracts the album art from an MP3 file and displays it in a PictureBox. Is there a better way to write this code? Dim file As TagLib.File = TagLib.File.Create(filepath) If file.Tag.Pictures.Length >= 1 Then Dim bin As Byte() = DirectCast(file.Tag.Pictures(0).Data...

What's the most efficient way to check the presence of a row in a table?

Say I want to check if a record in a MySQL table exists. I'd run a query, check the number of rows returned. If 0 rows do this, otherwise do that. SELECT * FROM table WHERE id=5 SELECT id FROM table WHERE id=5 Is there any difference at all between these two queries? Is effort spent in returning every column, or is effort spent in fil...

Is $_ more efficient than a named variable in Perl's foreach?

I am quite new in Perl and I woud like to know which of the following loops is more efficient: my @numbers = (1,3,5,7,9); foreach my $current (@numbers){ print "$current\n"; } or my @numbers = (1,3,5,7,9); foreach (@numbers){ print "$_\n"; } I want to know this in order to know if the use of $_ is more efficient because is...

Efficiency, Benchmarking, Speed-testing, Performance

Hi, I am trying to write a script whose efficiency I am trying to measure. I have a couple of questions:- For small applications, is this kind of profiling required? Or am I getting paranoid? (assuming most code is decently efficient/no infinite loops) Against what should I benchmark this? What should I compare against? Below is the e...

.net Garbage Collection and managed resources

Is the memory from primitive data types (int, char,etc) immediately released once they leave scope, or added to garbage collection for later release? consider: For x as integer=0 to 1000 dim y as integer Next If this doesn't add 1000 integers to the garbage collector for clean up later, how does it treat string objects? would this cr...

To check if String contains an element from a List (of Strings) - Is there a better way to write this code?

For the following block of code: For I = 0 To listOfStrings.Count - 1 If myString.Contains(lstOfStrings.Item(I)) Then Return True End If Next Return False The output is: Case 1: myString: C:\Files\myfile.doc listOfString: C:\Files\, C:\Files2\ Result: True Case 2: m...