Skip to Content
Julia for Data Analysis
book

Julia for Data Analysis

by Bogumil Kaminski
January 2023
Beginner to intermediate content levelBeginner to intermediate
472 pages
13h 21m
English
Manning Publications
Content preview from Julia for Data Analysis

Appendix B Solutions to exercises

Exercise 3.1

Create an x variable that is a range of values from 1 to 10^6. Now, using the collect function, create a y vector holding the same values as the x range. Using the @btime macro, check the time of sorting x and y by using the sort function. Finally, using the @edit macro, check the implementation of the sort function that would be invoked when you sort the x range.

Solution

julia> using BenchmarkTools
 
julia> x = 1:10^6;
 
julia> y = collect(x);
 
julia> @btime sort($x);
  1.100 ns (0 allocations: 0 bytes)
 
julia> @btime sort($y);
  7.107 ms (2 allocations: 7.63 MiB)
 
julia> @edit sort(x)

Observe that sorting the x range is much faster than sorting the y vector. If you have a properly configured Julia environment ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Use Gemini for Market Analysis

Use Gemini for Market Analysis

Uwe Weinreich

Publisher Resources

ISBN: 9781633439368Supplemental ContentPublisher SupportOtherPublisher WebsiteSupplemental ContentPurchase Link