Speed improvement tips with a little tech

C, C ++ (and more?) Story

Increment, decrement

General writing

int i = 1;
i++;

Such an increment is called a post-increment </ b>. When using a for statement

for(int i = 0;i < 10000;i++){
}

How to write a little faster

int i = 1;
++i

Such increments are called pre-increments </ b>.

When using a for statement

for(int i = 0;i < 10000;++i){
}

A little commentary

With post-increment </ b>, an instance is created internally, so it seems that it will take some time to process. However, due to the development of compilers, most of them are not available now. </ Font>

reference

Talking in C, C ++ and many other languages

Double writing

General writing

int num = 100;
int twice = num * 2;

How to write a little faster

int num = 100;
int twice = num << 1;

A little commentary

"Bitwise operations It is generally said that "it is faster", but I think that there are many cases where it is difficult to know when to actually use bit operations. Isn't the case that is the easiest to use in such a case twice the above? However, at first glance, it is often ??? (readability is reduced), so it may be better to consider the usage.

  • Right shift ( num >> 1, half, 1/2) is a signed (negative value) calculation, num / 2 and num >> The execution result may change with 1.
reference

List story

General writing

List<int> numList = new List<int>();
for(int i = 0;i < 10000;++i){
  numList.Add(i);
}
if(numList.Contain(9999)){
}

How to write a little faster

HashSet<int> numList = new HashSet<int>();
for(int i = 0;i < 10000;++i){
  numList.Add(i);
}
if(numList.Contain(9999)){
}

A little commentary

When using a variable length array such as C # or Java that corresponds to List (Vector in C ++), if you write a process to determine whether a value exists in the array, the process equivalent to the following process will be performed. I will.

public bool Contain(int value){
  for(int i = 0;i < this.Count;i++){
    if(this[i] == value){
      return true;
    }
  }
  return false;
}

For example, if there are 10000 data in this List, it will be checked up to 10000 times. (Such a process is called Linear Search Masu) If you use HashSet or Dictionary (HashMap etc. in Java), you can get the corresponding value in one process. Therefore, the speed is faster because the judgment is only once. (This kind of processing is called hash search. For details, see the reference.

reference

MySQL story

last

General writing

SELECT id FROM table name ORDER BY DESC LIMIT 1;

When using Rails, which is often used

ActiveRecord.last.id

How to write super fast

SELECT (information_schema.tables.AUTO_INCREMENT - 1) FROM information_schema.tables WHERE information_schema.tables.TABLE_NAME = 'table name';

Notes </ font>

A little commentary

Issuing a ORDER BY statement in SQL will slow down the process as the number of cases increases, even if it is `` `LIMIT 1```. (Basically, sorting process is heavy) By default, MySQL has a table that records meta information such as various definition values in MySQL, such as table information called INFORMATION_SCHEMA. It can only be read from this INFORMATION_SCHEMA table, and the value can be obtained by using the SELECT statement. The information of the table of INFORMATION_SCHEMA also records the information of the number of the table (TABLE_ROWS) and the information at the end (AUTO_INCREMENT). Therefore, it can be executed at high speed by getting the information from this table.

  • However, don't forget that there are some caveats.

Other

If there are other cases where the speed can be improved by just changing the writing style, I would like to add it.