LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-23-2020, 05:28 AM   #16
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019

Quote:
Originally Posted by vmelkon View Post
I think that
if (mode && newmode != mode)
is better than
if ((mode != 0) && (newmode != mode))

on the x86 platform. There is a special instruction that is used when you do if(mode).
Anyway, perhaps performance is not important in your case.
Personally, I'd use if (mode) when mode is a pointer or bool, and if (mode ==/!= something) otherwise. but that's just me.

Write for readability, let the compiler worry about the rest.
 
Old 01-23-2020, 07:44 AM   #17
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Quote:
Originally Posted by Geist View Post
I think the best and most performant code is the code that the programmers can the most easily read, understand and work with.
I hate typing but ... I use descriptive names and all the brackets and spaces I think are needed to make my code readable because the thing I hate more then typing is code I can't understand.

“The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming.”
- Donald Knuth "The Art of Computer Programming"
 
Old 01-23-2020, 09:01 PM   #18
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by vmelkon View Post
I think that
if (mode && newmode != mode)
is better than
if ((mode != 0) && (newmode != mode))

on the x86 platform. There is a special instruction that is used when you do if(mode).
Nope. Don't just sit in your armchair and think, try it.
 
Old 08-08-2022, 01:48 PM   #19
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by vmelkon View Post
I think that
if (mode && newmode != mode)
is better than
if ((mode != 0) && (newmode != mode))

on the x86 platform. There is a special instruction that is used when you do if(mode).
Anyway, perhaps performance is not important in your case.
There's now an easy way to test theories like that!

https://godbolt.org/

Spoiler: it makes no difference, and none of those variations cause any "special instruction" to appear.

Obviously, both variations get parsed into identical AST trees. Assembly generated from identical AST trees is identical.

Last edited by dugan; 08-08-2022 at 10:32 PM.
 
1 members found this post helpful.
Old 08-08-2022, 07:08 PM   #20
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
I don't think that I could have expressed the essential sentiment better than @geinst: (emphasis mine)

Quote:
I think the best and most performant code is the code that the programmers can the most easily read, understand and work with.

Computers are quite beastly these days, and [...] you should be fine just writing legible stuff, even if you're using old versions of compilers and whatnot from back then. [...]
At the end of the day, "you are writing for the programmers who will follow you." Remembering that those programmers might have utterly no idea just who you are or were. (They won't be able to glean any insights from the grass atop your grave ...) Therefore, "clarity is the key."

Personally, if I encountered source-code such as "if (mode && (mode...", I would have preferred "even more clarity." I would have preferred that the "OP = Original Programmer" first, explicitly, have disposed of the case where "&mode" might have possibly referred to "nothing."

In my opinion, the case where "mode refers to nothing" is distinct from the case where "[mode refers to something and ...] mode does not equal newmode."

"TMTOWTSI ... There's More Than One Way To Say It.™"

Last edited by sundialsvcs; 08-08-2022 at 07:18 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 05:00 PM
Can anyone please explain about "Function call interrupts" entry in /proc/interrupts? cyclops.xmen Linux - Software 2 12-09-2009 12:13 PM
can someone explain the 'for' function in python to me deathalele Programming 8 10-16-2008 11:10 AM
Help me ... explain parameters of cacheflush() function minhstone Red Hat 2 01-22-2008 09:41 PM
explain function Filipe Programming 1 03-01-2007 06:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration