Switch from Delphi/Pascal to Julia

Dear Julia users,

I am using an optimization model which is written in Delphi/Pascal and is called from a Python-Skript. However, Delphi (in my opinion) is not a very user friendly language and I would like to switch to a more modern open-source language. Python is too slow for my use case, but as I understood Julia should be as fast as Delphi. As a Python-user I still want something that “feels like Python”, i.e. is interactive, well documented, easy to read and write and has packages for easy csv-Import etc. That’s why I ended up with Julia :slight_smile:

My question now is: How would you start transfering the Delphi-Code to Julia? The Code has approximately 20.000 lines split in multiple units and functions, so I won’t be able to translate it all at once. Can I call Julia-functions from Delphi and should start translating function-by-function? Or is it easyer the other way round - calling Delphi-functions from Julia and start translating the “surrounding” code? Or do you think it’s not worth the effort and I should stick with Delphi?

I’m fairly new both to Delphi and Julia and have no experience in combining two programming languages in one project yet.

Thanks for your responses :slight_smile:

1 Like

Not exactly your question, but see Workflow for converting Python scripts - #3 by tamasgal

2 Likes

I am as usual going to say something stupid here. It looks like this code has been given to you, or you have taken over a project?
Maybe take a step back and ask what this optimization model is supposed to do.
Maybe there is a clean and easy way to write a model like that in Julia, with an existing Julia package.
I do not want to sound like a smartass here. All I am saying is take a step back before launching into a code conversion exercise.
Keep the existing ode and use it to verify your new Julia code?

ps. Welcome to Julia!

8 Likes

Well, probably you won’t need more then 2000 LOC in Julia for the same task, but that requires to rethink what the model is supposed to do…

thank you already for you’re replies!

The code is part of a project that I joined. So on the one hand, it was given to me, on the other hand it is still used by other people in my group. The model is an energy system model - so basically the function that get’s optimized. For the optimizer itself I’m sure I find an existing Julia package.

The energy system model is still used in different projects by other people and sometimes its further developed there as well. So switching language either needs to be very fast - or I always need a working model during the convertion time. That’s why I thought about transfering it to Julia step by step instead of rewriting everything at once.

I’d be happy to reduce the code to 2000 LOC :smiley:

I was thinking about something like that. But do you know if there is something similar like PyCall to call Delphi from Julia? Can you do something like that with CCall?

We had an old reactor model in Delphi. Rewrote into Julia. Speed-up 100x and much more maintainable. The only downside is not being able to easily compile to a DLL, but there are workarounds.

2 Likes

You can create dlls with delphi that can be called from C, and if they can be called from C then they can also be called from Julia. Not much documentation on how to do it available.

Example for calling Delphi dlls from R:
https://snoweye.github.io/R_note/inc_menu/reference/dfu/delphi_dlls_for_R.htm

Just try to create a simple example and we can help you to fix any problems you encounter…

3 Likes

@ufechner7

Delphi is one of the most productive languages. According to a study performed in 2017 by Namcook Analytics LLC, Delphi is in position 5 in the Function Point index, seriously outperforming its main competitor (C and C++).
Julia is two positions under Delphi. But that considering that the program does not need a GUI.
If you need a GUI, Julia… well… we all know…

So, always check facts before advising people :slight_smile:

2 Likes

Why do you think Delphi is not modern? In the last 5 years, Delphi had 7 major releases. Compare that with Julia’s releases. Also, why would you call Delphi “not modern” when it can do GUIs nativelly since 1994?
Isn’t Julia the one that cannot do natively GUIs?

(Never empty words, without citations, so) you can find release dates for Delphi here: https://www.amazon.com/Delphi-all-its-glory-programmers/dp/B0BZFRZQSM


One could say that Delphi is more hardware/business/GUI/gaming/web oriented while Julia is more data-orientated. But this is NOT what you said :slight_smile:

2 Likes

This thread just popped at in my account again so I wanted to use that opportunity to thank the community for your remarks and advises :slight_smile: We finally switched our model from Delphi to julia and it fulfilled our expectations. I indeed ended up translating the code and using the old model to verify. What I realised so far:

  • major downside: it was quite time consuming to get the new code to work.
  • data in- and output is much easier in julia
  • the profiler is very helpful. Naively translated the julia-code was slower, but with a little help of the profiler and the performance tips by now it’s about twice as fast as delphi. Digging deeper (on my list is StaticArrays, fastmath and in general hunting down allocations) I still hope for a potential performance increase in the order of 10-100
  • new development is much easier, i.e. writing additional functions and testing them outside the whole model. Partly that’s also due to that I had to clean the code, as julia is forcing me to get rid of global variables
  • also our master students can work with the code now. With the delphi-code, the entrance barrier was too high. As julia is quite understandable for people with a python background, it’s much easier to find qualified master students.
  • as julia is open source, we save money for Delphi licences
  • the support from julia discourse is very helpful! In general, up-to-date information that is relevant for our purpose is easier to find
  • packages that we need for dataframes, optimization algorithms, potentially plotting, performance profiling grow rapidly

So overall I’m very happy with the descision to switch to julia. Of course Delphi has it’s own purposes. But in our context julia seems to be fitting much better.

What remains is that I still need to call the major model from python, which feels quite clumpsy and I had a hard time to set the installation up and make it work. But translating the python-part will be much easier (less code :wink: )

29 Likes

That’s a nice feel-good story, glad it worked out for you! Out of interest, how many LOC did you end up with roughly?

3 Likes

Partly that’s also due to that I had to clean the code, as julia is forcing me to get rid of global variables

:slight_smile:
Delphi is NOT forcing people to use global variables, also. It is a pure user choice.
Of course, beginners (not only Delphi beginners) will abundantly use global vars :slight_smile:

PS: Similarly, Delphi has also pointers. They are there. But you are not forced to have to use them. Again: user’s choice.

as julia is open source, we save money for Delphi licences
I thought Delphi is also free for academia… or?
Embarcadero Academic Program - Embarcadero

Anyway, personally I think $1500 for a PERMANENT license is a very small price, considering the 500 visual components and 17 lines of code (RTL, libraries, etc) delivered with Delphi.
When you make the money back (from the software sold) in a week, then the price is really really small.


Anyway. Julia is cool also.
Probably Julia will be embraced by the world once it will be able to produce executable files to give it to your users/customers…

1 Like

I didn’t count them, but they are still a lot - they might have even increased as now we are automatically formatting the code, which leads to additional line breaks. Overall when translating the code, I followed the principle 1) make it work, 2) make it fast (enough) and in the end 3) make it nice, i.e. clean the code, make it more “julian”. The third step is still work in progress :wink:

@AnalogGuy I never said that Delphi is forcing me to do something - just julia is forcing me to not do something, which has been done in our Delphi code. Of course there are many situations where Delphi might be the right choice - but in our case julia suits much more our needs. As a non-university research institution, we have to pay thousands of euros for the licences per person (either named or more costly concurrent licences), which is hard to get refunded through public projects. Not buying enough concurrent licences can lead to bottlenecks for our master students… And additionally, physics and engineering students don’t learn Delphi or C, but just come with a basic python background, which makes it easier to get into julia than Delphi. And we neither need GUIs nor executable files :slight_smile:

9 Likes