Playback speed
×
Share post
Share post at current time
0:00
/
0:00

Paid episode

The full episode is only available to paid subscribers of Computer, Enhance!

Writing a Simple Haversine Distance Processor

Now that we have input for it, it's time to write a (slow) haversine distance processor we can use as our initial codebase.

This is the second video in Part 2 of the Performance-Aware Programming series. Please see the Table of Contents to quickly navigate through the rest of the course as it is updated weekly. The reference haversine function (listing 65) is available on the github.

In the previous post, we made a generator that would provide input to our eventual Haversine processing routine. Now that we've got the input, it's time to make the actual routine.

Part of this routine is very simple for us to write because we already wrote the haversine distance function as part of the reference average in the generator. That part of the processor will be nothing more than a simple cut-and-paste from the generator code we already have.

But despite having already written some of the code, there is actually a lot of new work involved in today's homework — and all of it is due to the fact that we have to parse JSON.

JSON is actually a very complicated thing to input into a program compared to raw binary data. It’s easy to forget just how much extra code you need to read JSON data, compared to the essentially-zero code you need to use raw binary.

Most people use libraries to parse JSON, so they never see just how much extra work the CPU has to do. But this course is specifically about learning how much work the CPU has to do! So we don’t get to use a library this time. We have to parse the JSON ourselves.

Now JSON actually isn’t that much work to parse compared to more complicated text languages. A simple JSON parser is a few hundred lines of code or less, whereas something that could fully parse, say, real C++ would be much more involved.

But JSON obviously is a lot more work than just loading raw binary data, which is zero lines of code — you just load the file, and then you have the data. And part of the required work in this course is experiencing that difference in practice, since it is an excellent example of the kinds of programming choices developers make every day without understanding the ramifications.

So perhaps you are used to reading JSON with something like this:

The full video is for paid subscribers

Computer, Enhance!
Programming Courses
A series of courses on programming topics.