premature-optimization

Calling a method on an object a bunch of times versus constructing an object a bunch of times

I have a List called myData and I want to apply a particular method (someFunction) to every element in the List. Is calling a method through an object's constructor slower than calling the same method many times for one particular object instantiation? In other words, is this: for(int i = 0; i < myData.Count; i++) myClass someObje...

How to test what method implementation runs faster

While the question check if input is type of string has been closed two of the answers spiked a micro-optimization question in my mind: which of the below two solutions would perform better? Reed Copsey provided a solution using Char.IsLetter: string myString = "RandomStringOfLetters"; bool allLetters = myString.All( c => Char.IsLetter...

Java Filters Performance Question

I have two questions. The first is do Filters add a lot of overhead to request. We have a filter and it is set to run on the URL pattern /*. This means it also runs on all the image request. I think that this is not good for performance, but my co-workers think that it doesn't matter if the filter runs 5 or 6 times per request becaus...

Remove the delimiter , at the end

String prefix = ""; for (String serverId : serverIds) { sb.append(prefix); prefix = ","; sb.append(serverId); } The following code runs faster than the above code . the "," prefix object does unnecessary object creation on every iteration . The above code takes 86324 nano seconds,while mine takes only 68165 nano seconds. List<St...

Premature optimization or am I crazy?

I recently saw a piece of code at comp.lang.c++ moderated returning a reference of a static integer from a function. The code was something like this int& f() { static int x; x++; return x; } int main() { f()+=1; //A f()=f()+1; //B std::cout<<f(); } When I debugged the application using my cool Visual Studio debugger ...

What optimizations are premature?

I've been here for nearly a month and it seems that people have a tendency to be eager to use the "Premature Optimization is the root of all evil" argument as soon as someone mentions efficiency. What is really a premature optimization? What is the difference between what is essentially writing a well designed system, or using certain m...

How to write more efficient code

Question of the century? I basically want to know which would be more efficient if I wrote this code as several different variables or if I used small arrays. int x = 34; int y = 28; int z = 293; vs double coordinate[3] = {34, 28, 293}; I have a coordinate struct which I will use in the following way: typedef struct coordinates_t ...

Are Java static calls more or less expensive than non-static calls?

Is there any performance benefit one way or another? Is it compiler/VM specific? I am using Hotspot. ...

Why do Java and C# have bitshifts operators?

Is the difference between integer multiply(temporarily forgetting about division) still in favor of shifting and if so how big is the difference? It simply seems such a low level optimization, even if you wanted it the shouldn't the (C#/Java) to bytecode compiler or the jit catch it in most cases? Note: I tested the compiled output for...

ColdFusion: More efficient structKeyExists() instead of isDefined()

Which of these is more efficient in ColdFusion? isDefined('url.myvar') or structKeyExists(url, 'myvar') ...