premature-optimization

How to write more efficient code

雨燕双飞 提交于 2019-12-03 17:19:45
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 { double x = 0.0; double y = 0.0; double z = 0.0; } coordinates; typedef struct car_t { coordinates start; // car starting point coordinates location; // car current Location coordinates targCarVector; // Vector to car from target coordinates altitude; // Altitude of

Calling a getter multiple times or calling once and assigning to a variable?

試著忘記壹切 提交于 2019-12-03 11:56:01
问题 say suppose I have class as : public class Age { private int age; public int getAge() { return this.age; } } In my Main class I am calling the getAge() method many times. So I wanted to know is it advisable to call so many times or call once and assign it to some variable and use that variable. Which is best and why? 回答1: This is likely a situation where you are optimizing before you know you need to. The value is just an integer so it is not taking up a lot of memory if you store the value

Why would this Lua optimization hack improve performance?

非 Y 不嫁゛ 提交于 2019-12-03 08:10:51
I'm looking over a document that describes various techniques to improve performance of Lua script code, and I'm shocked that such tricks would be required. (Although I'm quoting Lua, I've seen similar hacks in Javascript). Why would this optimization be required: For instance, the code for i = 1, 1000000 do local x = math.sin(i) end runs 30% slower than this one: local sin = math.sin for i = 1, 1000000 do local x = sin(i) end They're re-declaring sin function locally. Why would this be helpful? It's the job of the compiler to do that anyway. Why is the programmer having to do the compiler's

Calling a getter multiple times or calling once and assigning to a variable?

随声附和 提交于 2019-12-03 02:14:12
say suppose I have class as : public class Age { private int age; public int getAge() { return this.age; } } In my Main class I am calling the getAge() method many times. So I wanted to know is it advisable to call so many times or call once and assign it to some variable and use that variable. Which is best and why? This is likely a situation where you are optimizing before you know you need to. The value is just an integer so it is not taking up a lot of memory if you store the value in multiple places. At the same time, it is a very simple method call that will not take much time to execute

Shear a numpy array

試著忘記壹切 提交于 2019-11-30 19:12:06
I'd like to 'shear' a numpy array. I'm not sure I'm using the term 'shear' correctly; by shear, I mean something like: Shift the first column by 0 places Shift the second column by 1 place Shift the third colum by 2 places etc... So this array: array([[11, 12, 13], [17, 18, 19], [35, 36, 37]]) would turn into either this array: array([[11, 36, 19], [17, 12, 37], [35, 18, 13]]) or something like this array: array([[11, 0, 0], [17, 12, 0], [35, 18, 13]]) depending on how we handle the edges. I'm not too particular about edge behavior. Here's my attempt at a function that does this: import numpy

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

血红的双手。 提交于 2019-11-30 16:27:21
问题 Which of these is more efficient in ColdFusion? isDefined('url.myvar') or structKeyExists(url, 'myvar') 回答1: These days (CF8+) the difference in speed is not that great. However, structKeyExists is indeed a little faster. Here's why. When you use isDefined , the string you pass in is searched for as a key name in several scopes. As of CF9, the list of scopes, in the order checked is: (source) Local (function local, UDFs and CFCs only) Arguments Thread local (inside threads only) Query (not a

x > -1 vs x >= 0, is there a performance difference

时光毁灭记忆、已成空白 提交于 2019-11-30 07:52:41
I have heard a teacher drop this once, and it has been bugging me ever since. Let's say we want to check if the integer x is bigger than or equal to 0. There are two ways to check this: if (x > -1){ //do stuff } and if (x >= 0){ //do stuff } According to this teacher > would be slightly faster then >= . In this case it was Java, but according to him this also applied for C, c++ and other languages. Is there any truth to this statement? There's no difference in any real-world sense. Let's take a look at some code generated by various compilers for various targets. I'm assuming a signed int

Am I understanding premature optimization correctly?

百般思念 提交于 2019-11-30 05:46:55
问题 I've been struggling with an application I'm writing and I think I'm beginning to see that my problem is premature optimization. The perfectionist side of me wants to make everything optimal and perfect the first time through, but I'm finding this is complicating the design quite a bit. Instead of writing small, testable functions that do one simple thing well, I'm leaning towards cramming in as much functionality as possible in order to be more efficient. For example, I'm avoiding multiple

Shear a numpy array

一世执手 提交于 2019-11-30 03:32:36
问题 I'd like to 'shear' a numpy array. I'm not sure I'm using the term 'shear' correctly; by shear, I mean something like: Shift the first column by 0 places Shift the second column by 1 place Shift the third colum by 2 places etc... So this array: array([[11, 12, 13], [17, 18, 19], [35, 36, 37]]) would turn into either this array: array([[11, 36, 19], [17, 12, 37], [35, 18, 13]]) or something like this array: array([[11, 0, 0], [17, 12, 0], [35, 18, 13]]) depending on how we handle the edges. I

Why swap doesn't use Xor operation in C++

☆樱花仙子☆ 提交于 2019-11-29 18:01:04
问题 I've learned that Xor operation can be used to implement effective swap function. like this: template<class T> void swap(T& a, T& b) { a = a^b; b = a^b; a = a^b; } But the implementation of swap all i can found on the internet is essentially like this: template<class T> void swap(T& a, T& b) { T temp(a); a = b; b = temp; } It seems that the compiler didn't generate the same code for the two form above because I tested it on VC++ 2010 and the first one is done the job more quickly than std: