When Should I Use C?
Explore when to choose C programming by understanding its advantages in speed, memory control, and system-level access. Learn how C excels in scientific computing, processing large data efficiently, and programming IoT devices. By the end, you will grasp when C is the right tool for performance-driven and resource-constrained tasks.
We'll cover the following...
As we learned in the previous lesson, C has an edge over many languages in that it’s fast, has fine-grained access to the system, and has a small memory footprint. This makes C a language of choice for scientific applications, system programming, and
Let’s look at these in more detail.
C for scientific programming
You should think of C as one of many tools in your toolkit for performing computational tasks in your scientific work. Take advantage of C when the situation calls for it. Use other languages when ease of use and maintainability are the goal or when the ecosystem developed around those languages provides features essential for your task.
In a lab we are affiliated with, we use Python, R, and (sometimes) MATLAB, but when we feel the need for speed, we use C.
Processing large data
C may not be the best choice for interactive data exploration, like when we want to load in some data, plot it in different ways, do some rudimentary calculations, plot the results, etc. For this sort of interactive exploratory scripting, a language like Python, MATLAB, R, etc., may be sufficient. In particular, these other languages make it easy to quickly generate good-looking graphics.
However, for cases where we need to process a large amount of data, you’ll find that these languages are slow. Even for fairly common statistical procedures, like bootstrapping (techniques that involve resampling data thousands or tens of thousands of times), interpreted languages will be orders of magnitude slower than C.
In such situations, C starts looking very attractive. If you have a data processing operation or a simulation, and you know it will take a long time to run, then spending some time implementing it in C is often worth it. The graph below compares the speed of interpreters for several languages. As you can see, C shines in this regard.
Why care?
The rule of thumb is that if we have to wait more than about 10 seconds to see the result of a calculation or operation, we should start considering implementing it in C.
Who cares if a calculation takes 10 seconds or 30 seconds, not five minutes, for that matter? Are five minutes so bad? The answer is, no, it’s not so bad if you only have to do it once, but it’s almost never the case that you perform a computation on your data only once.
Imagine writing some code to read in data from one subject, process that data, and write the result to a file, and that operation takes 60 seconds. Is that so bad? Not if you only have to run it once.
Now let’s imagine you have 15 subjects in your group. Your 60 seconds turn into 15 minutes.
Now let’s say you have four groups with 15 subjects in each group. In that case, 15 minutes turn into one hour.
You run your program, have lunch, and come back an hour later, and you find there was an error. You fix the error and rerun…another hour. Even if you get it right, imagine your supervisor asks you to rerun the analysis five different ways, varying some parameter of the analysis (maybe filtering the data at a different frequency, for example). Now you need five hours to see the result.
It doesn’t take a massive amount of data to run into this sort of a situation. If you program the same data processing tasks in C, and you achieve a 100x speedup (which is not unusual), now those five hours turn into 180 seconds (you could run your analysis twice, and it would still take less time).
C for system programming and IoT devices
System and device programming is another area where C is usually the language of choice. Such programming often requires direct access to memory and hardware resources, which is a strong suit of the C language. In addition, C is fast, making it well-suited for developing high-performance system software (such as operating systems and device drivers) that might need to process gigabits of data per second in addition to executing several other core tasks.
C’s small memory footprint and ability to allow fine-grained control over system resources, such as memory, make it an ideal choice for programming IoT devices. These devices are usually small and have limited resources, making C ideal.
We’ve learned a lot about C’s behavior and efficiency. In the next lesson, we’ll conclude this discussion and move on to writing code.