DEV Community

Cover image for The Peregrine programming language - A Python-like language that's as fast as C.
Ethan Olchik
Ethan Olchik

Posted on • Updated on

The Peregrine programming language - A Python-like language that's as fast as C.

Hey guys!

I'm Ethan, I'm one of 10 Peregrine developers. This post is gonna be about some updates we've added into Peregrine lately.

About

If you know Python, you know how easy it is. However, it also comes with a big downgrade. Python is slow, and I'm pretty sure every python developer knows this by now. This is kind of annoying. That's where Peregrine comes in. Me and 8 other friends have been working on Peregrine for the past few months. Peregines syntax is very similar to Python's, and it gets trans-compiled to C, thus making it as fast as C. Below I've written 2 programs, one in Peregrine and one in Python.

Peregrine

def fib(int n) -> int :
    if n <= 0:
        return 1
    return fib(n-1) + fib(n-2)

def main():
    count = 0 # Peregrine has type inference!
    int res = 0

    while count < 40:
        res = fib(count)
        count++
Enter fullscreen mode Exit fullscreen mode

function return types can be omitted.

Python

def fib(n):
    if n <= 0:
        return 1
    return fib(n-1) + fib(n-2)

res = 0
for c in range(0, 40):
    res = fib(c)

Enter fullscreen mode Exit fullscreen mode

These two programs are almost the same, which makes it so easy for Python users to switch to. Now, you might be asking: "How much faster is Peregrine?" Well, to answer your question, here are the results:

Peregrine:

Executed in: 1.06 secs

Python:

Executed in: 32.30 secs

As you can see, Peregrine is significantly faster than Python. It is around 30x faster than python, without optimization when running this program.

What's new?

Here are some of Peregrine's newest features:

Type Inference

Type Inference is one of Pergrine's newest features. This allows Peregrine code to be written with simplicity.

if/else/match

Although this may seem like a standard feature in any programming language, it does take time to add these features which is why I'm acknowledging it. Not much to say about it since it's in every programming language.

New-ish Features

Let's talk more about the features that are currently available in Peregrine.

Ccode

Ccode allows C code to be ran in Peregrine. Here is an example:

def main():
    x = 1
    Ccode x++; Ccode
    print("{x}\n") # prints 2
Enter fullscreen mode Exit fullscreen mode

As you can see, any variables declared outside the Ccode block can be used within Ccode and vice versa. This also means you can import any C library through Ccode and use it in Peregrine.

Inline Assembly

You can also have inline assembly in Peregrine. Here is an example:

def main():
    int arg1 = 45
    int arg2 = 50
    int add = 0
    print("It should add 45 and 50 using asm and print it\n")
    asm("addl %%ebx, %%eax;" : "=a" (add) : "a" (arg1) , "b" (arg2))
    printf("%lld", add)
Enter fullscreen mode Exit fullscreen mode

This prints 90, as expected.

More

You can find some more examples in the Peregrine test folder

Planned Features

  • Structs
  • More decorators for different purposes
  • Python ecosystem in Peregrine
    • You will be able to use any python module in Peregrine

Conclusion

Peregrine is planned to release version 0.0.1 sometime in March, so make so to show some support by starring the repo and make sure to press on the "Watch" button so you don't miss any updates.

We would greatly appreciate any contributions, so if you find something that you can improve, open a pull-request! You can also check out our open issues

Thanks so much for reading!

Discussion (98)

Collapse
otumianempire profile image
Otu Michael

can you also support x:int since you have int x though x can be inferred. That way all that I'd need to convert python to swallow is main method. 🙏

Collapse
saptakbhoumik profile image
SaptakBhoumik • Edited

Ok 👍👍

Collapse
toddrjen profile image
Todd

I came here to say this. With python already supporting a syntax for types there isn't much point rolling your own.

Collapse
cipharius profile image
Valts Liepiņš

Nim language project seems to be very similar to your goals.

Have you looked into that project? Wouldn't Nim's metaprogramming engine be able to reproduce the planned features in your project?

Collapse
saptakbhoumik profile image
SaptakBhoumik

Yes I know about nim but there are some diffrences like this project tries to replecate the good things of python and C but nim even if inspired by python does not try to replecate python

Collapse
cipharius profile image
Valts Liepiņš

The difference between Nim and Python syntax-wise is approximately same as difference between the given examples of Swallow compared to Python. Yes, variable and procedure declaration is slightly different, but type signature syntax in Nim is more similar to Python's compared to Swallow.

The main question here is why is there a need for new programming language, when a very similar, more mature project exists? It would be more fruitful to improve an already existing project and that would bring greater good to programmers looking for Python like C language.

But of course, if the project's main motivation is learning about language design, parsing, compiling and just for fun, then it makes more sense. Since developing and launching a new language is a very challenging and risky task that will take years of maintenance and improvements, and even then it most likely will not get the traction between programmers to stay alive.

Thread Thread
otumianempire profile image
Otu Michael

I have heard of Nim but to be honest I have not tried it. Seeing swallow, I think it is much more like C. It is as if I am writing C but sugar coated with python. I am writing C using python syntax (This sounds better). That is how I see it. I don't know much about language design, parsing, compiling or the rest but I have used python and JavaScript more as a back-end developer. So looking at Swallow (python-coated-C), it would stay alive, maybe in a small niche but it will still stay alive if it will revolve around python - for that I can say. I stand to be corrected.

Thread Thread
cipharius profile image
Valts Liepiņš

Nim also compiles to C, so it's same as Swallow in that manner. Besides Nim is already released language where as Swallow isn't past v1 yet.

Also Nim can compile to JS too.

Thread Thread
saptakbhoumik profile image
SaptakBhoumik

why is there a need for new programming language, when a very similar, more mature project exists?

Thats like saying to the creater of c that why make a new language when fortain already exists

Since developing and launching a new language is a very challenging and risky task that will take years of maintenance and improvements, and even then it most likely will not get the traction between programmers to stay alive.

We didnt ask you to take risk. We are taking

Thread Thread
cipharius profile image
Valts Liepiņš

Obviously the project doesn't affect me in any way.

I simply believe that greater things can be achieved when great minds work on common goal.

Good luck with the project!

Thread Thread
geekbro profile image
Geek

Let them do whatever they want with the project. Let's wait and see whether the outcome and improvements of the project benefits us over time. By the way, the fact is, in most cases especially in our technology domain, great minds (even if they have common goals) don't work together. If it happened, we would have not got different programming languages, operating systems and so on. The goals might change over time. It's better we let them do whatever they want as long as the outcome of their work is going to benefit everyone in the niche. And, although Nim and swallow seem to have common goals for now, we don't know what other goals the swallow team has in its list. They might come up with some additions and improvements over time.

Thread Thread
kerberosmorphy profile image
KerberosMorphy • Edited

Why doing a todo app in react since there's so much todo app... I'm pretty sure the main idea behind Swallow/Peregrine is to do something they want to, they are passionate about and they will be proud.
It's normal to talk about want you're proud of, maybe it is the same thing than Nim, but if you dont make a programming language, you'll never know how to make one... and if it's what they want to learn, this is a great way to do it!
Is this programming language have a futur? Only the futur will tell you
Are the programmers behind that have a futur? Clearly, they are passionate, will learn a lot, want to give to the community, 10/10 they have a future.
And if a beginner want to contribute to this project, it could be easier to him since it's in an early phase and could be easier to understand.
This is not a company looking to take over part of a market, this are individuals looking to learn and share.
So go! Follow your passion and your goal!

Thread Thread
cipharius profile image
Valts Liepiņš

The second reply in this thread already expressed the same idea:

But of course, if the project's main motivation is learning about language design, parsing, compiling and just for fun, then it makes more sense.
Enter fullscreen mode Exit fullscreen mode
Collapse
careuno profile image
Carlos Merchán

looks very interesting

did you develop a GC or how are you handling memory?

Collapse
saptakbhoumik profile image
SaptakBhoumik

For memory management, our plan is to use auto-free where the compiler will insert free calls wherever necessary

Collapse
Sloan, the sloth mascot
Comment deleted
saptakbhoumik profile image
SaptakBhoumik • Edited

I know it is going to be difficult

Thread Thread
along1x profile image
Andrew Long

I think this will be the biggest sticking point for many devs. The statement from your readme:

It will have no garbage collector because it is a system programming language but it will be very easy to use so there will be less chance of a memory leak

That seems to be a pretty bold assertion; there's a reason why the most popular programming languages today all use a garbage collector. Even using a garbage collector, I've come across many leaky applications in my career. Proving that your compiler can understand when a free call is "necessary" will be a big lift for you.

That said, I'm rooting for the cause here, and curious to see how it pans out...

Collapse
junux profile image
JUNUX1

Hi there, nice work I really like what your doing.
Just a question is in my mind, why u guys don't work on julia programming language?
It's easy, fast and with a powerful support.
And oh it's already created so u don't have to work from zero right?😅

Collapse
saptakbhoumik profile image
SaptakBhoumik

Both of them have diffrent goals

Collapse
ranitbandyopadhyay profile image
Ranit Bandyopadhyay

Just the programming language that python programmers needed for competitive programming. Can you please ensure if swallow is supported by online judge

Collapse
saptakbhoumik profile image
SaptakBhoumik

I dont think that they will disqualify swallow but please note that this language is still not complete

Collapse
thanitorial profile image
Greg Cramb

Quick question Gui and swallow using python libs like tkinter etc... Will this come around with the planed feature of python ecosystem? If so I can see my self adopting this for a project I am working on at the moment😎

Collapse
saptakbhoumik profile image
SaptakBhoumik • Edited

That's the plan

Collapse
greenroommate profile image
Haris Secic • Edited

Given the idea that Python is cross-platform - in the idea where you use Python libs for your own code, how does this compare? I know Python and other languages as well use libraries written in C or Rust or such, then just make interface for such calls which again makes code unusable on some platforms where those dependencies weren't "ported". So I assume you would still have to compile this for each platform you want to run on? The only difference being that now you could write everything in one language and use source includes then recompile everything together where Python libs written for a specific platform "cannot" do this because they pre-compiled for single platform. It's really niche experience and not many libs are like that but still I'm wondering

Collapse
saptakbhoumik profile image
SaptakBhoumik

Well the library that run on a single platform in python will run on a single platform on swallow as well

Collapse
greenroommate profile image
Haris Secic

I was wondering more about cross-compile issues with the same language, not using Python lib in Swallow. As text clearly points out is that you may now be able to avoid Python libraries written in other languages for speed and then having interface to Python and instead simply have libraries fully written in same language as the code you're writing yourself. Point being you can write Python application and move it's code anyplace without thinking about platform unless you depend on such libraries. If swallow will have to be compiled each time for platform then it looses that perspective. Note that not all Python apps are written to use AI stuff or something depending on heavy calculations so they don't care about this and enjoy "write once move anywhere" to some extent. Again not saying it's preventable in all cases but if you completely loose that part of the Python then you loose specific group of devs.

Collapse
ajob41 profile image
Ayub

Ethan please change the name if you only know that naming is important.
Please change it Rubik I love ruby since Ruby is dynamic if you add it the letter K will signify as for statice type language
Like Ruby(dynamic typed) Rubik or Rubyk(static typed).

Collapse
ethanolchik profile image
Ethan Olchik Author

We are discussing name changing right now

Collapse
kerberosmorphy profile image
KerberosMorphy

Since the comparison, I would have call it Cobra XD.
But yeah you need a name and file extension that are marketable.

Collapse
ajob41 profile image
Ayub

Because swallow is not marketable like , Shawshank redemption the movie, everyone agrees the movie is one of the best movies of all time, but the name killed the movie.

Collapse
robinsond7691 profile image
Robinson • Edited

Hey guys, i never post comments in this site but I logged in just to add to the argument: if you guys have the intention of the language being widely adopted once it matures, please, greatly consider a name change. Here in the United States, the word swallow has a very strong sexual connotation and I'm absolutely sure that people will be embarrassed to say they code in Swallow. I'm surprised someone else in the comments said that the word also has a bad meaning in their language (not English).
Again, i wish you success, but I guarantee you the language will not pick up in the United States just because developers will not be throwing that word around.

Collapse
ethanolchik profile image
Ethan Olchik Author

OK, I'll discuss this.

Collapse
jmarshall9120 profile image
jmarshall9120

How is trans compiling to see C, going to be different from what Python is doing? Unless I'm mistaken this is what CPython is doing, it's just doing it in real time, aka interpreting. And we can force Python, to compile instead of interpret already. Am I missing something here?

Collapse
evanoman profile image
Evan Oman

You're not missing anything, just a lot of hype over something new and shiny (with nice branding and graphics on the GitHub page)

Collapse
gaggerytsai profile image
GaggeryTsai

.. Python could resolve the performance issue with @lru_cache for the memorization to improve the example of Fibonacci. It is matter of memory consumption. That would be great if you can show both the performance and memory consumption comparison between Python and Swallow for this example to show case how good the Swallow language is. Otherwise I doubt Swallow is using more memory.

Collapse
saptakbhoumik profile image
SaptakBhoumik

Well using @lru_cache will be cheating. If you run a multithreaded python program and a simgle threaded c program then it is very likely that python will win

Collapse
iamludal profile image
Ludal 🚀

Awesome project! 🙌

Collapse
ethanolchik profile image
Ethan Olchik Author

thanks!

Collapse
mustapha profile image
Mustapha Aouas

Amazing project ! Don’t loose your time and energy with the haters and keep up the good work ;)
I’ll definitely keep an eye on this repo

Collapse
ethanolchik profile image
Ethan Olchik Author

Thanks for the support!

Collapse
jonwillcode profile image
Jonathan Williams

Wonderful project! Can't wait to see updates

Collapse
ethanolchik profile image
Ethan Olchik Author

thanks!

Collapse
pinghajen profile image
pingpong1109

This just makes me wish someone would make a Python compiler to do just this but as a drop in replacement instead of a similar syntax separate language

Collapse
out0 profile image
out0

The issue with speed is memory management, also the fact that python is interpreted. Once compiled, it runs faster, but still it will be slower due to GC and other memory features.
C is blasing because it demands the programmer to know computer science.

It isnt a language for newbies or people that had studied only by YouTube videos or online courses. It's a language for those that understand how a computer works.

But that comes with a price. It's much more prone to errors and hard to learn.

Having said that, I praised these guys for their boldness into trying to develop such a language, but I guess it will be hard or even impossible to come up with a solution without a GC to self memory management...
Well, take a look at objective-C. It might help

Collapse
joseprelat profile image
JosepRelat

Hi guys! Very interesting idea. I use Python to develop scientific applications. Pythonis great bcoz of the scientific modules but it's a pain to develop applications with it. Particularly, i
I'd love to see:

1) No GIL. There are plenty of solutions for multiprocessing pools for data analytics, but Python is excruciatingly painful when creating concurrent threads that require intercommunication, which is the core of any application. I'd love to be able to create threads easily without having to spawn a whole new process. Also, that it us possible to have memory-shated spaces without the annoyance of using ctypes, which is not native. In reality, I feel thst the true problem here more than the GIL is dynamic typing, but I'm not an expert.

2) Portability of scientific libraries. I know you have it in the pipeline, but thus is the real advantage of python to me.

3) runtime enviornmemt to deploy applications, which is totally lacking.

I know I might be in a very small niche, but I feel there is a need there for building applications that require scientific-level data processing. I know that the standard solution to this problem is:

1) write the app in java/C#/C++ n embed compiled c code from python/Matlab scripts for the data processing bits. But that works well only if the data processing is contained. Otherwise, it's a lot of interface. For a big company, they can afford to have a deducated team for translation, but startups, those that usr python the most, cannot.

2) use scijava or other numerical packages. But those suck.

Python is great because it allows to go from analytics to production, except that production sucks.

Best Regards and Best of luck!

Collapse
amieeriddler profile image
AmieeRiddler

The powerful programming language that is also easy to learn, Python has fewer keywords and more free English language syntax whereas C is more difficult to write. Hence, if you want an easy development process go for Python. Speed-wise C is a better option. spell to make people break up

Collapse
tecnosam profile image
Abolo Samuel Isaac

This looks awesome, if after the planned update it's able to work with flask and/or Django, I'll try building a project with it to see how it goes.

Will it also support data science libraries too?

Collapse
saptakbhoumik profile image
SaptakBhoumik • Edited

Will it also support data science libraries too?

100% but the basic language is still not complete

Collapse
otumianempire profile image
Otu Michael

I am looking for to it...

Collapse
emileo profile image
EmileO

Cython

Collapse
saptakbhoumik profile image
SaptakBhoumik

Swallow and cython are diffrent with diffrent goals. Swallow will have many more features

Collapse
pjotterplotter profile image
pjotterplotter

How does Cython transpilation to c differ from Peregrinne?

Thread Thread
saptakbhoumik profile image
SaptakBhoumik

Pointers, constant etc etc. Also we are not making yet another python to c converted

Thread Thread
pjotterplotter profile image
pjotterplotter

Cool, and let's say I'd want to use it for developing games, can I "link"/integrate a c lib like glfw and compile to webassembly?

Thread Thread
saptakbhoumik profile image
SaptakBhoumik

Yes you can

Collapse
luccahuguet profile image
Lucca Huguet • Edited

Why not just contribute to julia which is very similar but much more mature?

What would be Swallow's edge over julia?

Collapse
saptakbhoumik profile image
SaptakBhoumik

Well swallow is a systems programming language and it's goal is diffrent from julia

Collapse
irsoft profile image
Iván Rodriguez Saiz

If Swallow is similar to VLang, it seems great to me, this one is inspired by Go and does not have a production version yet but for what it is doing so far it seems great to me. In this case, if swallow is inspired by Python, it also seems great to me, a language that is compiled, without GC, with auto-free and has the advantages that Python has, would be magnificent. It seems absurd to me to try to compare them at this time when it is just being born.

Collapse
luccahuguet profile image
Lucca Huguet

Hmm interesting

Collapse
amitkum12611780 profile image
Amit kumar

This site is so amazing, This sites gives good knowledge of python , This is very helpful for me.Here all content so useful and helpful for beginner.

Collapse
fultonm profile image
Michael Fulton

I was not aware Python was significantly slower than C in production. Usually people use Cypthon in production right

Collapse
along1x profile image
Andrew Long

What makes Python faster or slower in a production environment vs. a language like C isn't the choice of the interpretter per say (CPython vs PyPy), but the efficiency of certain design patterns or usage of highly optimized libraries in your production code. For example, if you're able to vectorize many of your operations (using numpy or pandas, for example), your code can perform much faster regardless of your interpretter selection.

Collapse
fultonm profile image
Michael Fulton • Edited

Interpreted will be slower than compiled in general because there’s more layers of execution between the code and CPU at runtime. Holding constant the design patterns and workload, Cython can equal C at runtime efficiency. Unlike CPython, Cython is a superset of Python (and C) which gets compiled with a standard C/C++ compiler. If a Python solution needs to be put into production with minimal effort, it’s often easier to compile it with Cython than rewrite it in a systems language.

Collapse
saptakbhoumik profile image
SaptakBhoumik

A native solution is better

Collapse
jeromek13 profile image
Jérôme Krell

Hey cool Project, but why are you using the V?

Collapse
saptakbhoumik profile image
SaptakBhoumik

We are using V because it is fast, easy to use and works in the same way as swallow works

Collapse
yukendhiran profile image
yukendhiran

If you going to create a package maneger name it as (Hatcher) it will suitable for peregrine

Collapse
saptakbhoumik profile image
SaptakBhoumik

Cool name

Collapse
zephyrstack profile image
Zephyr-stack • Edited

I am not able to find an extension of this language on vs code, is there any?
Also, will it be compatible with packages say numpy, tensorflow, flask etc?
Awesome effort btw

Collapse
ethanolchik profile image
Ethan Olchik Author

not yet

Collapse
aidantilgner profile image
Aidan Tilgner

This seems like a really awesome project! Looking forward to trying it out.

Collapse
otumianempire profile image
Otu Michael

Ccode x++; Ccode does it have to be on one line? Or is it a block. If it is a block, can it be imported (I will put whatever C code into a c file and then import/include it where I need it)?

Collapse
saptakbhoumik profile image
SaptakBhoumik

Alt text
You can put whatever you want inside it ;)

Collapse
karthikbhandary2 profile image
Karthik Bhandary

I really think that keeping the syntax similar to python is better. It will be an easy shift from the language

Collapse
alfiensugh profile image
alfie-nsugh

A python like language where you can seamlessly integrate assembly and c!? That's incredible I'm looking forward to trying this out!

Collapse
saptakbhoumik profile image
SaptakBhoumik

Thanks:)

Collapse
flormaa profile image
FLORMAA • Edited

very cool