Click here to Skip to main content
15,886,919 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: I like C more than I thought I would Pin
sasadler15-Sep-22 6:14
sasadler15-Sep-22 6:14 
GeneralRe: I like C more than I thought I would Pin
honey the codewitch15-Sep-22 6:19
mvahoney the codewitch15-Sep-22 6:19 
GeneralRe: I like C more than I thought I would Pin
rjmoses17-Sep-22 23:39
professionalrjmoses17-Sep-22 23:39 
GeneralRe: I like C more than I thought I would Pin
honey the codewitch17-Sep-22 23:49
mvahoney the codewitch17-Sep-22 23:49 
GeneralRe: I like C more than I thought I would Pin
rjmoses19-Sep-22 0:29
professionalrjmoses19-Sep-22 0:29 
GeneralRe: I like C more than I thought I would Pin
Clive Hudson23-Sep-22 5:52
Clive Hudson23-Sep-22 5:52 
GeneralRe: I like C more than I thought I would Pin
honey the codewitch23-Sep-22 7:09
mvahoney the codewitch23-Sep-22 7:09 
AnswerPrograming gut feeling - ** Update! ** Pin
megaadam13-Sep-22 23:37
professionalmegaadam13-Sep-22 23:37 
I am not asking for absolute knowledge, I am not asking you to Google for me. And I know I could measure the answer myself. I just wonder whether you share my gut feeling on this.

During a review yesterday I came across: (The language here is Go, but I would argue the same way in C etc. And uint64() is compile-time)
C++
gopNr = reqGopNr - entryStart + uint64(entry.Offset)
if gopNr >= uint64(entry.assetLen) { // Avoid mod operation every time since wrap is unusual
    gopNr = gopNr % uint64(entry.assetLen)
}


I commented:
I do not think % can have a measurable cost for small divisors. I would skip the if. It is a single IDIV operation in X86. If that is expensive, there might even be an if in the operator already...


Shooting from the hip, what is your gut feeling?

** Update! **
Thanks for all the interesting feedback.
So I did measure it: Go Playground - The Go Programming Language[^] For some reason the code always measures zero or times out on that playground, but measures fine locally.

The verdict is:
Running with if is in fact faster, if we stick to the original assumption that the divisor is almost always smaller.

The difference is a blazing 10 nanoseconds or if you prefer factor ~4x on an old x86 laptop. I was wrong thinking that it would not be measurable. But this will run on monster server and this is not the most frequently visited code. So I still vote to remove the if for the sake of readability.
"If we don't change direction, we'll end up where we're going"


modified 14-Sep-22 12:15pm.

AnswerRe: Programing gut feeling Pin
honey the codewitch13-Sep-22 23:39
mvahoney the codewitch13-Sep-22 23:39 
AnswerRe: Programing gut feeling Pin
CPallini14-Sep-22 0:27
mveCPallini14-Sep-22 0:27 
AnswerRe: Programing gut feeling Pin
OriginalGriff14-Sep-22 0:44
mveOriginalGriff14-Sep-22 0:44 
GeneralRe: Programing gut feeling Pin
honey the codewitch14-Sep-22 1:03
mvahoney the codewitch14-Sep-22 1:03 
AnswerRe: Programing gut feeling Pin
Greg Utas14-Sep-22 1:07
professionalGreg Utas14-Sep-22 1:07 
AnswerRe: Programing gut feeling Pin
Randor 14-Sep-22 1:21
professional Randor 14-Sep-22 1:21 
GeneralRe: Programing gut feeling Pin
harold aptroot14-Sep-22 5:52
harold aptroot14-Sep-22 5:52 
GeneralRe: Programing gut feeling Pin
Randor 14-Sep-22 6:26
professional Randor 14-Sep-22 6:26 
AnswerRe: Programing gut feeling Pin
PIEBALDconsult14-Sep-22 3:29
mvePIEBALDconsult14-Sep-22 3:29 
AnswerRe: Programing gut feeling Pin
Dan Neely14-Sep-22 3:56
Dan Neely14-Sep-22 3:56 
GeneralRe: Programing gut feeling Pin
  Forogar  14-Sep-22 5:48
professional  Forogar  14-Sep-22 5:48 
AnswerRe: Programing gut feeling Pin
harold aptroot14-Sep-22 5:44
harold aptroot14-Sep-22 5:44 
GeneralRe: Programing gut feeling - ** Update! ** Pin
jsc4214-Sep-22 22:55
professionaljsc4214-Sep-22 22:55 
GeneralWSO CCC OTD 2022-09-14 Pin
OriginalGriff13-Sep-22 21:47
mveOriginalGriff13-Sep-22 21:47 
GeneralRe: WSO CCC OTD 2022-09-14 Pin
pkfox13-Sep-22 22:37
professionalpkfox13-Sep-22 22:37 
GeneralRe: WSO CCC OTD 2022-09-14 Pin
DerekT-P13-Sep-22 23:06
professionalDerekT-P13-Sep-22 23:06 
GeneralRe: WSO CCC OTD 2022-09-14 - we have a winner! Pin
OriginalGriff13-Sep-22 23:12
mveOriginalGriff13-Sep-22 23:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.