Recent

Author Topic: Compiler case optimization vs if then else  (Read 5408 times)

damieiro

  • Full Member
  • ***
  • Posts: 200
Re: Compiler case optimization vs if then else
« Reply #15 on: December 11, 2019, 08:26:22 am »
Quote
There is one book I always recommend to someone who is interested in how a compiler works, it is: Brinch Hansen on Pascal Compilers.  Unfortunately, it is out of print and, the price people are asking for it is outrageous but, I haven't seen a better introductory book on compilers.   If you can get a copy at a reasonable price, I recommend it very highly.

I take your advice, thanks again.


Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Compiler case optimization vs if then else
« Reply #16 on: December 11, 2019, 09:19:07 am »
Quote
There is one book I always recommend to someone who is interested in how a compiler works, it is: Brinch Hansen on Pascal Compilers.  Unfortunately, it is out of print and, the price people are asking for it is outrageous but, I haven't seen a better introductory book on compilers.   If you can get a copy at a reasonable price, I recommend it very highly.

I take your advice, thanks again.
I would get Wirth's books on Pascal.
A bit silly: most good  literature about compilers are language agnostic.
Specialize a type, not a var.

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: Compiler case optimization vs if then else
« Reply #17 on: December 11, 2019, 05:12:31 pm »
   If you can get a copy at a reasonable price, I recommend it very highly.

Incredible : used > 170.- $,  new  > 800.- $

Or some PDF-bandits: give us your credit card number ...

Winni


Ouch!!!  Well, then, I'm glad I bought it earlier this year (also upon a recommendation from 444BX) when the price was much better.


-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

440bx

  • Hero Member
  • *****
  • Posts: 3945
Re: Compiler case optimization vs if then else
« Reply #18 on: December 11, 2019, 06:17:53 pm »
Ouch!!!  Well, then, I'm glad I bought it earlier this year (also upon a recommendation from 444BX) when the price was much better.
I'm pleased you got it at a reasonable price.  I hope you've enjoyed it as much as I did.

Kind of funny, that book is almost an investment by now.  <chuckle>
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Compiler case optimization vs if then else
« Reply #19 on: December 11, 2019, 06:32:25 pm »
Ok.
most good  literature about compilers are language agnostic.

See < >:D >:D >:D >:D >:D >:D>

Programmers who write books about  their programming language as if it was a relegion are useless.
« Last Edit: December 11, 2019, 06:35:58 pm by Thaddy »
Specialize a type, not a var.

440bx

  • Hero Member
  • *****
  • Posts: 3945
Re: Compiler case optimization vs if then else
« Reply #20 on: December 11, 2019, 06:59:43 pm »
Ok.
most good  literature about compilers are language agnostic.

See < >:D >:D >:D >:D >:D >:D>

Programmers who write books about  their programming language as if it was a relegion are useless.
Calm down!... all those devilish feelings you have inside you are bad for you.

Just for the record and revealing what is obvious to most, Pascal is not my programming language.  Probably the only ones who can make that claim are Kathleen Jensen and Niklaus Wirth.

As far as the religion thing goes, neither Jensen, Wirth nor Hansen wrote a book called "the church of Pascal". If there were any religious overtones in their books, I totally missed them.  On the other hand, books from The Jehova's witnesses are another story altogether (not much on compilers in those books though.)

Also, language agnostic books usually go into great detail about the various parsers and how to implement them but, all that theory does very little to educate the potential compiler writer on how to design a language.  Actually, it might do more damage in that area than good.

All that said, you are entitled to your opinion and I will _not_ argue the subject with you.  Thank you for sharing your strong feelings and unfounded opinions.  :)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Compiler case optimization vs if then else
« Reply #21 on: December 11, 2019, 07:35:04 pm »
It is a fact. Not an opinion.
Specialize a type, not a var.

440bx

  • Hero Member
  • *****
  • Posts: 3945
Re: Compiler case optimization vs if then else
« Reply #22 on: December 11, 2019, 07:37:13 pm »
It is a fact. Not an opinion.
Goes without saying you think so.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Compiler case optimization vs if then else
« Reply #23 on: December 11, 2019, 07:49:16 pm »
A bit silly: most good  literature about compilers are language agnostic.

I agree a bit with 440bx here. Too general books are often superficial, and don't really make choices.

In general I'd go with an applied book, and save the general standard works for later deepening and abstracting  of the knowledge.

syntonica

  • Full Member
  • ***
  • Posts: 120
Re: Compiler case optimization vs if then else
« Reply #24 on: December 12, 2019, 03:10:56 am »
Quote
You can see that in the asm code

Well, my fault. I rephrase the question: do the compiler the same intermediate code for the case and the if then else? Do some optimizations? Does the compiler detect better optimization with the case statement?.
(not asking even for other optimizations depending of data type, for example if "my var" were an string)
A good compiler will easily transform your excessive case statement into a jump table if it makes sense to do so. But, if your case statement is at a bottleneck in your code, you may need to rethink if you can make that selection beforehand and factor it out of your time-critical loop.

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: Compiler case optimization vs if then else
« Reply #25 on: December 13, 2019, 12:20:16 am »
Kind of funny, that book is almost an investment by now.  <chuckle>


I thought that very thing... :)
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

damieiro

  • Full Member
  • ***
  • Posts: 200
Re: Compiler case optimization vs if then else
« Reply #26 on: December 13, 2019, 08:12:44 am »
Well, many thanks to all of you.

@Thaddy: I take on Wirth books too. I have read some of them, but i neved had read Hansen books

@syntonica:
Quote
A good compiler will easily transform your excessive case statement into a jump table if it makes sense to do so. But, if your case statement is at a bottleneck in your code, you may need to rethink if you can make that selection beforehand and factor it out of your time-critical loop.
I was talking about simple cases to be translated without jump table if not really needed.
On the other hand, i was only talking about compiler performance and what documentation says. I think we all assume that programmer's work must be done too.

@all: thanks for all your comments.

 

TinyPortal © 2005-2018