Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Julia High Performance

You're reading from   Julia High Performance Optimizations, distributed computing, multithreading, and GPU programming with Julia 1.0 and beyond

Arrow left icon
Product type Paperback
Published in Jun 2019
Publisher Packt
ISBN-13 9781788298117
Length 218 pages
Edition 2nd Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Avik Sengupta Avik Sengupta
Author Profile Icon Avik Sengupta
Avik Sengupta
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Julia is Fast FREE CHAPTER 2. Analyzing Performance 3. Types, Type Inference, and Stability 4. Making Fast Function Calls 5. Fast Numbers 6. Using Arrays 7. Accelerating Code with the GPU 8. Concurrent Programming with Tasks 9. Threads 10. Distributed Computing with Julia 11. Licences
12. Other Books You May Enjoy

How fast can Julia be?

The best evidence of Julia's performance claims is when you write your own code. We encourage you to run and measure all the code snippets in the book. To start, we will provide an indication of how fast Julia can be by comparing a similar algorithm on multiple languages.

As an example, consider the algorithm to compute a Mandelbrot set. Given a complex number, z, the function computes whether, after a certain number of iterations, the  function converges or not. Plotting the imaginary numbers where that function diverges on a 2D plane produces the following iconic fractal image that is associated with this set:

The following code computes the divergence point based on this logic. Calling this function over all points on a 2D plane will produce the Mandelbrot set:

function mandel(c)
z = c
maxiter = 80
for n in 1:maxiter
if abs(z) > 2
return n - 1
end
z = z^2 + c
end
return maxiter
end

You will notice that this code contains no type annotations, or any special markup, even though it operates on complex numbers. It looks remarkably clean, and the idea that the same mathematical operations can apply to many different kinds of mathematical objects is key to Julia's expressiveness.

The same algorithm implemented in modern C would look as follows:

int mandel(double complex z) {
int maxiter = 80;
double complex c = z;
for (int n = 0; n < maxiter; ++n) {
if (cabs(z) > 2.0) {
return n;
}
z = z*z+c;
}
return maxiter;
}
Downloading the example code:
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by taking the following steps:
  1. Log in or register on our website using your email address and password
  2. Let the mouse pointer hover over the SUPPORT tab at the top
  3. Click on Code Downloads & Errata
  4. Enter the name of the book in the Search box
  5. Select the book for which you're looking to download the code files
  6. Choose from the drop-down menu where you purchased this book
  7. Click on Code Download
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of the following:
  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

By timing this code in Julia and C, as well as re-implementing it in many other languages (all of which are available within the Microbencmarks project at https://github.com/JuliaLang/Microbenchmarks), we can note that Julia's performance claims are certainly borne out for this small program. Plotting these timing results in the following chart, we see that Julia can perform at a level similar to C and other statically typed and compiled languages:

This is, of course, a micro benchmark, and therefore cannot be extrapolated too much. However, I hope you will agree that it is certainly possible to achieve exceptional performance in Julia, without having to fall back to low-level languages for performance-critical code. The rest of the book will attempt to show how we can achieve performance close to this standard, for many different kinds of code bases. We will learn how to squeeze the maximum possible performance from the CPU, without any of the overhead that typically plagues dynamic languages.

You have been reading a chapter from
Julia High Performance - Second Edition
Published in: Jun 2019
Publisher: Packt
ISBN-13: 9781788298117
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at R$50/month. Cancel anytime