Compiled with user input vs scripting language

In summary, the conversation discusses the potential use of Fortran as a scripting language, specifically for repetitively running time-consuming models with different parameters. The main question is whether using Fortran as a scripting language would be more efficient than using an interpreted language like Python or MATLAB. The conversation also touches on the idea of compiling data with the Fortran program versus reading in a data file and the potential performance differences between the two methods. It is concluded that in general, a Fortran program will run faster than an interpreted language equivalent due to the native executable code produced by the compiler and linked libraries. However, there is no open source equivalent to this concept and the efficiency may depend on the specific use case and data involved. The conversation also brings
  • #1
geouke
9
0
I am going to use Fortran and Python as examples of a compiled and a scripting language in this question.

If I write a Fortran program and compile it, it is going to process data more quickly than a python script that does the same thing. However, what if I write a Fortran program that allows user-supplied data without re-compiling. For example, 'program_name data.dat' at the console would feed a data file into a Fortran program that would process it and spit out an answer. An even more general example would be if I write a Fortran console program that allows a user to define variables, do special functions, plot easily, etc. In other words, I have created a scripting language with Fortran. These are my questions:

(1) If a data file is read in using 'program_name data.dat' rather than the data being compiled into an executable and ran, have I lost some execution speed other than the time it takes the program to read the data? Why?

(2) It takes program_name.f03 time 't1' to compile and run and it takes 'program_name data.dat' time 't2' to read data and execute, is t1 <. ==. or > t2? Why?

I am in a graduate program where I need to do potentially time-consuming models repetitively with different parameters. The question of whether to use Fortran or Python for small tasks is obvious, but I am wondering what I have gained by using Fortran. More importantly, I would like to know why I have gained.

Thanks! You guys are always great.
 
Technology news on Phys.org
  • #2
So the question is can you create a scripting language run from a Fortran compiled program that is more effecient that the Python interpreter on the same machine? Seems one of the issues would be the efficiency in parsing scripts in Fortran, which could depend on library functions to assist parsing, possibly written in assembly, but callable from Fortran.

Another alternative would be a scripting language that tokenizes user input as it's entered or editted, similar to how functions are defined in some versions of Basic.

Then there's the issue that even if the end result is more efficient, will it make up for the time you spent developing it.
 
Last edited:
  • #3
That is part of what I am asking. Maybe it would be more clear if I remove Python and say that I am comparing a compiled Fortran program which has data compiled with it to one that acts on a data file. Let's say my problem involves a large matrix in several dimensions and could take days or weeks to run. Furthermore, it depends on data that is fed into it.

Scenario 1: All of the data is read into the program when it is compiled.
Scenario 2: Only the processes that act on the data are compiled and a data file is read in.

Scenario 2 is basically like a scripting language. Let's say my program opens with a prompt that I type, "set datafile 'data.dat'". It is behaving like MATLAB or Python now.

In scenario 1, time is taken to read in the data during compilation and some set of instructions is carried out on it.
In scenario 2, time is taken AFTER compilation to read in data and the same instructions are carried out on it.

So, factoring in the extra time it takes to compile in scenario 1, is the overall time still less? If so, why?
 
  • #4
In general, the FORTRAN program is going to run faster than an interpreted language equivalent and people have been writing FORTRAN programs to read in data files and process them since it first appeared - I don't remember ever writing a 'serious' program that had the data compiled with it. I suspect that the compile and link time in a modern system is going to be in the order of seconds and, if you intend to run the program many times without further modification, then you can probably ignore it once you've got it working. It may be slower to develop a working program in FORTRAN because of the compile-link overhead but the end performance gains may be worth (particularly if you have a program that will take a long time to run).

The reason it's quicker in general is that the compiler and linked libraries produce native executables for the machine you are running the program on and the code is not re-evaluated every time you run it, unlike a standard interpreter (although some interpreters effectively compile on the fly, look up Just In Time interpreters)
 
  • #5
So, let me make sure this statement is correct:

Let's say I wrote a program in Fortran that can process the equivalent of a Python or MATLAB script and distributed as .f source code, it would run more quickly than MATLAB or Python (assuming that same math underlied the routines being done) because it was compiled on each system rather than being installed from an executable? If that is the case (that is, assuming I understand correctly), why is there no open source equivalent to what I am talking about?
 
  • #6
does it take longer for a MATLAB script to execute because the computer has to convert the script into a language that it understands? Also, does that take longer than actually executing the code in some cases?

x = ones(1000,1000,1000)
x = rand(x)
x = erf(x)

Now, let's say I have a Fortran program that does the same thing. Is the speed terribly different even though MATLAB does not have much to interpret? Is the interpreting process more involved than I am imagining?
 
  • #7
If the code is fixed (although it may have conditionally executed sections) and only the data changes, then it would be faster to compile the code one time, and use it on many sets of data files, as oppposed to including the data sets, as header files, and recompiling for each dataset.

If the data set includes crude script like features, sort of like a programmable calculator, then the issue would be the significance of the parsing / scripting overhead versus the actual operations to be performed.

Another alternative would be the equivalent of "compiling" the scripting part of the datasets into some form of tokens that would reduce the scripting overhead from the Fortran generated program.
 
  • #8
geouke said:
does it take longer for a MATLAB script to execute because the computer has to convert the script into a language that it understands? Also, does that take longer than actually executing the code in some cases?

x = ones(1000,1000,1000)
x = rand(x)
x = erf(x)

Now, let's say I have a Fortran program that does the same thing. Is the speed terribly different even though MATLAB does not have much to interpret? Is the interpreting process more involved than I am imagining?
I'm not a Matlab user, but as far as I'm aware, Matlab is specialized for matrix and calls C or FORTRAN libraries to handle many of the built-in array operations. It is possible that the execution time for such a simple program will be comparable to a compiled program, as the interpreter does indeed have very little to do. J (derived from APL) is another interpreted array language and that is claimed to have very good performance for such problems.

You'll need the 64-bit version of Matlab running on 64-bit Windows or Linux to handle that size array.
 

FAQ: Compiled with user input vs scripting language

1. What is the difference between compiling with user input and using a scripting language?

Compiling with user input involves taking code written in a high-level language and converting it into machine code that can be directly executed by the computer. Scripting languages, on the other hand, are interpreted at runtime and do not go through a compilation process. This means that code written in a scripting language can be modified and executed without the need for compilation.

2. Which is better for creating programs, compiling with user input or using a scripting language?

The answer to this question depends on the specific needs and goals of the program. Compiling with user input is generally better for creating large, complex programs that need to run efficiently, while scripting languages are better suited for smaller, simpler programs that require quick development and flexibility.

3. Are there any performance differences between compiled code and code written in a scripting language?

Yes, there can be significant performance differences between compiled code and code written in a scripting language. Compiled code is optimized for efficient execution, while interpreted code may have to go through additional steps at runtime, resulting in slower performance.

4. Can code written in a scripting language be compiled?

Some scripting languages, such as Python and Ruby, have compilers available that can convert the code into a more efficient form, but they still retain some of their interpreted characteristics. Other scripting languages, like JavaScript, do not have a traditional compilation process and rely on just-in-time (JIT) compilation at runtime.

5. Is it possible to use both compiled code and scripting languages in the same program?

Yes, it is possible to combine compiled code and scripting languages in the same program. This is often done in larger programs where certain parts need to be optimized for performance while others require the flexibility and ease of use provided by scripting languages.

Back
Top