LINUX.ORG.RU

История изменений

Исправление hateyoufeel, (текущая версия) :

Нет, это делается потому, что в современных CPU, конструкция switch/case сильно неэффективна из-за большого количества сравнений, которые не позволяют эффективно использовать branch predictor.

Не только. Это быстрее потому что:

The difference between the two is obviously the «bounds check» step of the switch. Why is it required? You may think that this is because of the default clause, but that isn’t true. Even without the default clause, the compiler is forced to generate the bounds check for the switch statement to conform to the C standard. Quoting from C99:

If no converted case constant expression matches and there is no default label, no part of the switch body is executed.

Therefore, the standard forces the compiler to generate «safe» code for the switch. Safety, as usual, has cost, so the switch version ends up doing a bit more per loop iteration.

Из ссылки в моём предыдущем комменте. Ну и branch prediction тут роляет, да. Код не перестаёт быть говнохаком, а сишечка как обычно сосёт.

Исправление hateyoufeel, :

Нет, это делается потому, что в современных CPU, конструкция switch/case сильно неэффективна из-за большого количества сравнений, которые не позволяют эффективно использовать branch predictor.

Не только. Это быстрее потому что:

The difference between the two is obviously the «bounds check» step of the switch. Why is it required? You may think that this is because of the default clause, but that isn’t true. Even without the default clause, the compiler is forced to generate the bounds check for the switch statement to conform to the C standard. Quoting from C99:

If no converted case constant expression matches and there is no default label, no part of the switch body is executed.

Therefore, the standard forces the compiler to generate «safe» code for the switch. Safety, as usual, has cost, so the switch version ends up doing a bit more per loop iteration.

Из ссылки в моём предыдущем комменте. Ну и branch prediction тут роляет, да. Сишечка как обычно сосёт.

Исправление hateyoufeel, :

Нет, это делается потому, что в современных CPU, конструкция switch/case сильно неэффективна из-за большого количества сравнений, которые не позволяют эффективно использовать branch predictor.

Нет, тебя обманули. Это быстрее потому что:

The difference between the two is obviously the «bounds check» step of the switch. Why is it required? You may think that this is because of the default clause, but that isn’t true. Even without the default clause, the compiler is forced to generate the bounds check for the switch statement to conform to the C standard. Quoting from C99:

If no converted case constant expression matches and there is no default label, no part of the switch body is executed.

Therefore, the standard forces the compiler to generate «safe» code for the switch. Safety, as usual, has cost, so the switch version ends up doing a bit more per loop iteration.

Из ссылки в моём предыдущем комменте. Ну и branch prediction тут роляет, да. Сишечка как обычно сосёт.

Исправление hateyoufeel, :

Нет, это делается потому, что в современных CPU, конструкция switch/case сильно неэффективна из-за большого количества сравнений, которые не позволяют эффективно использовать branch predictor.

Нет, тебя обманули. Это быстрее потому что:

The difference between the two is obviously the «bounds check» step of the switch. Why is it required? You may think that this is because of the default clause, but that isn’t true. Even without the default clause, the compiler is forced to generate the bounds check for the switch statement to conform to the C standard. Quoting from C99:

If no converted case constant expression matches and there is no default label, no part of the switch body is executed.

Therefore, the standard forces the compiler to generate «safe» code for the switch. Safety, as usual, has cost, so the switch version ends up doing a bit more per loop iteration.

Из ссылки в моём предыдущем комменте. Сишечка как обычно сосёт.

Исходная версия hateyoufeel, :

Нет, это делается потому, что в современных CPU, конструкция switch/case сильно неэффективна из-за большого количества сравнений, которые не позволяют эффективно использовать branch predictor.

Нет, тебя обманули. Это быстрее потому что:

The difference between the two is obviously the «bounds check» step of the switch. Why is it required? You may think that this is because of the default clause, but that isn’t true. Even without the default clause, the compiler is forced to generate the bounds check for the switch statement to conform to the C standard. Quoting from C99:

If no converted case constant expression matches and there is no default label, no part of the switch body is executed.

Therefore, the standard forces the compiler to generate «safe» code for the switch. Safety, as usual, has cost, so the switch version ends up doing a bit more per loop iteration.

Сишечка как обычно сосёт.