I know I said I would stick to C++, but…

I recently had a job interview where I would be performing FEA (dream job!), and I was asked about my knowledge of Python and VBA. Being that VBA is so very different from C++, I will most likely be able to keep it away from this blog but Python is actually quite close to C++. At least on the surface. On the surface I was convinced that it was only syntax differences. But it’s syntax differences that make it a different language is it not? Well there are other differences, but mostly the answer is yes. It’s a bit like saying English is like German and I should be able to speak German because I can speak English. Well, while it’s not a direct translation English is said to be a highly Germanic language. Basically what I’m trying to say is this: the words are the biggest difference. When it comes to these two languages, they are similar enough that once you know the words you can often arrange them in a similar manner. So it seems to be with C++ and Python.

From what I’ve seen so far and from my understanding of the histories of the programming languages, C++ is German and Python is English. You know what, before I dig too deep into this spoken language to programming language analogy how about I just provide some examples and talk about the differences there. Going against what I said last week, I rewrote my tension program. In sticking with what I said last week, it was very different. For reference my C++ tension program ended up like this:

1 // Simple program to calculate tension
2
3 //#include
4 #include
5 #include
6 using namespace std;
7
8 int m1,m2;
9 double g(9.81), T, numerator, denominator;
10
11 int main()
12 {
13     cout<>m1;
15     cout<>m2;
17     numerator = 2*m1*m2;
18     denominator = m1+m2;
19     cout<<"Final tension is:"<<endl;
20     T = (numerator/denominator)*g;
21
22     cout<<T<<endl;
23
24     return 0;
25 }

So I just want to point out the number of lines this program required in C++. I’ll include comment lines and “useless” whitespace in the count because the Python program has similar lines. Just not so many of them. Here it is:

1 #!/usr/bin/python
2 #simple program to find tension
3
4 m1 = input(” Enter a number for m1: “)
5 m2 = input(” Enter a number for m2: “)
6 g = 9.81
7
8 numerator = 2*m1*m2
9 denominator = m1+m2
10 T = (numerator/denominator)*g
11
12 print “Final Tension is “, T

Half the lines! I would venture to say this is at least as easy as MatLab if not easier. Aaaand since I made that statement, I just had to test it. Here’s what I threw together in Octave (Linux equivalent to MatLab) for comparison:

1 %simple matlab program for tension
2
3 m1 = input(‘Enter a value for m1: ‘);
4 m2 = input(‘Enter a value for m2: ‘);
5 g = 9.81;
6
7 numerator = 2*m1*m2;
8 denominator = m1+m2;
9 T = (numerator/denominator)*g;
10
11 printf(‘The final tension is ‘, T, ‘\n’)

So yes, actually very similar. One of the biggest things about Python (that I’m not sure I’ve warmed up to just yet) is the lack of need for an end of line operator or semi-colon (;). One of the first things you’ll read anywhere about Python is the fact that whitespace becomes a bit more important than other languages. I haven’t written enough programs to really see/understand how it really plays out compared to other languages, so I’ll report back after a bit more Python scripts are under the belt.
Otherwise I can definitely see the allure of Python at this level. This particular program is simple enough that the difference in run time is not perceptable and the writing of the program was a whole lot simpler. At this level Python even appears to be a much closer translation from MatLab than C++ could ever think about being. Maybe the analogy could be UK English to American English. There is a reason for this, though. My understanding is they are on the same “level” as computer languages go. Neither Python or MatLab need to be compiled to create executables the way C++ does. They are translated straight from source and therefore will typically not communicate with the computer as closely. And the cost of this speed on the back end of more calculation heavy programs. But the difference in the time saved on the front end can be seen in this very simple example.
All this to say this: I may start posting comparisons to Python in every post. The hardest part of this process is the learning, so if it turns out I can actually guess most of the Python syntax based on MatLab then I can have those written much faster than the C++ programs. There is also the fact that I don’t foresee my programs becoming so CPU intensive that there is a noticable difference in run time between the versions any time soon, so I won’t lose anything there either. This sounds like it could easily become a commercial for Python but we shall see as I go. If this excercise convinces me to use Python instead of C++ then it was because Python proved its merit in side-by-side testing.

As always, I will try to have another one of these by next week. One thing that will stay true next week is I will be moving to iterative methods and equations. Also as always, I welcome feedback and suggestions so feel free to leave them.

Advertisements

2 thoughts on “I know I said I would stick to C++, but…

  1. Yes! I had heard all the talk about the greatness and ease of Python but hadn’t made it around to trying it. As I did in the post, I stick to comparing it to MatLab in ease of front end writing and testing. That said, it does make me worry about run time with more complicated, computation intensive stuff based on my experience with MatLab. Definitely going to be part of my testing in the slightly more distant future.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s