Python vs C
Published: 2021-07-23 18:01:35

I'm writing this post to illustrate my personal learning experience around runtime speed, High and low-level programming languages, and their pros and cons.

I work as a junior system administrator for the university manage around 600 Linux machines, 7000 users, 350 Unix groups, and a handful of net groups. A few months ago, we decided to migrate our directory services from Network Information Services(NIS) to Light Weight Directory Access Protocol(LDAP). I was asked to write a migration script and test it on the database.

I was excited to write such a critical piece of infrastructure and worked hard using the PythonLdap API to get all the correct fields set up in LDAP. After a few weeks, my script was ready for production, migrating all the users and their data fields, the Unix groups, and net groups to the new directory server. The idea was that the script would keep LDAP and our old database in sync. If something changed on LDAP, the changes would be reflected on NIS and vise-versa. 

## Speed

The script worked great. Except for that, it took about 5min to iterate through both databases. I looked into optimizations, checked my data structures, and reduced runtime by about 1min. Not fast enough. We would call the script to run every time a change is made to the NIS database to update LDAP. 

My boss told me he wanted me to try and rewrite the script in C. I was bummed out about this at first because I had spent so much time working on the python script. It felt like all my work debugging and optimizing the script was for nothing. However, after some time, I got excited about the opportunity to dive deeper into a low-level programing language. C is essential and still widely used in industry. 

## Lessons Learned

Python compiles at runtime. On average C runs about 45 times faster than python because it is a compiled low-level language. Python is an interpreted language, meaning one line at a time is translated into machine code. Python has some advantages in ease of use and syntax, but C allows the developer to become familiar with memory architectures and delicate grained nuances of systems like buffer overflows. 

Both languages have their place in the world. If you are building a lightweight automation script, python works just fine. But if you want to write a piece of network infrastructure, you should write in a language with a lot of granularity and speed. If you are pursuing a Career in Networking or cyber Security, I recommend including both a scripting language and a low-level language in your tool kit.

### My recommendations for scripting languages
- [Python](https://www.python.org/)
- [bash](https://www.gnu.org/software/bash/manual/bash.html)
- [Perl](https://www.perl.org/)
- [php](https://www.php.net/)

### My recommendations for low level languages:
- [C](https://www.toptal.com/c/the-ultimate-list-of-resources-to-learn-c-and-c-plus-plus) Note: The official documentation is not very user friendly but there are plenty of open resources available
- [Rust](https://www.rust-lang.org/) Note: Rust is a newer precompiled language but it becoming widely adopted very quickly. It has some security advantages over C and is favored highly. 
- Depending on what you are doing [Assembly](https://docs.oracle.com/cd/E19253-01/817-5477/817-5477.pdf) is also very granular