premature-optimization

Why do Java and C# have bitshifts operators?

南笙酒味 提交于 2021-02-16 16:59:49
问题 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 C#(with gmcs Mono C# compiler version 2.6.7.0) and the multiply examples didn't use shift for multiplying even when multiplying by a multiple of 2. C# http:/

Java: Performance of Enums vs. if-then-else

走远了吗. 提交于 2019-12-29 06:50:14
问题 I've had no real luck getting a concise answer for this comparison by using Google and rather than do my own time consuming evaluations, I thought I would ask first. I'm fairly sure that a switch statement using Enums would perform faster than an if-then-else statement, though whether or not it is a noticable difference is another question. Could someone shed some light on this for me? Thanks for the quick responses guys, I will keep this in mind for future projects. 回答1: Yeap, it does,

Why don't people use xor swaps? [closed]

泪湿孤枕 提交于 2019-12-28 19:16:19
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I read on a site that using xor swaps is fast because it doesn't use a temporary variable. Here's an example: #include <stdio.h> int

perl string catenation and substitution in a single line?

时光总嘲笑我的痴心妄想 提交于 2019-12-24 09:04:48
问题 I need to modify a perl variable containing a file path; it needs to begin and end with a forward slash (/) and have all instances of multiple forward slashes reduced to a single slash. (This is because an existing process does not enforce a consistent configuration syntax, so there are hundreds of config files scattered everywhere that may or may not have slashes in the right places in file names and path names.) Something like this: foreach ( ($config->{'backup_path'}, $config->{'work_path'

Is ++i really faster than i++ in for-loops in java?

ε祈祈猫儿з 提交于 2019-12-17 15:53:30
问题 In java I usually make a for-loop like following: for (int i = 0; i < max; i++) { something } But recently a colleague typed it so: for (int i = 0; i < max; ++i) { something } He said the latter would be faster. Is that true? 回答1: No, it's not true. You could measure the performance by timing each loop for a large number of iterations, but I'm fairly certain they will be the same. The myth came from C where ++i was regarded as faster than i++ because the former can be implemented by

Efficiently taking the absolute value of an integer vector in C

喜你入骨 提交于 2019-12-11 03:28:05
问题 The task is to set each element of a C integer array to its absolute value. I'm trying to do it as efficiently as possible. Below are a progression of optimizations that I've made. Please tell me if these are actually optimizations at all, and if any more can be made! The first parameter of the function will be an integer array, and the second will be an integer size of that array. Here's the standard implementation: void absolute (int array[], int n){ for(int i = 0; i < n; i++) if(array[i] <

Is count(*) really expensive?

亡梦爱人 提交于 2019-12-10 00:50:11
问题 I have a page where I have 4 tabs displaying 4 different reports based off different tables. I obtain the row count of each table using a select count(*) from <table> query and display number of rows available in each table on the tabs. As a result, each page postback causes 5 count(*) queries to be executed (4 to get counts and 1 for pagination) and 1 query for getting the report content. Now my question is: are count(*) queries really expensive -- should I keep the row counts (at least

How to write more efficient code

不羁岁月 提交于 2019-12-05 02:20:26
问题 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

Is count(*) really expensive?

吃可爱长大的小学妹 提交于 2019-12-04 22:43:35
I have a page where I have 4 tabs displaying 4 different reports based off different tables. I obtain the row count of each table using a select count(*) from <table> query and display number of rows available in each table on the tabs. As a result, each page postback causes 5 count(*) queries to be executed (4 to get counts and 1 for pagination) and 1 query for getting the report content. Now my question is: are count(*) queries really expensive -- should I keep the row counts (at least those that are displayed on the tab) in the view state of page instead of querying multiple times? How

Why would this Lua optimization hack improve performance?

做~自己de王妃 提交于 2019-12-04 11:49:00
问题 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