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 sure 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!

Latest comments (97)

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
 
yukendhiran profile image
yukendhiran

Can peregrine able to use java,python,C++ native library?

Collapse
 
saptakbhoumik profile image
SaptakBhoumik

you mean ffi?

Collapse
 
yukendhiran profile image
yukendhiran

Yes

Thread Thread
 
saptakbhoumik profile image
SaptakBhoumik

Yes you can

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
 
anthonyjohnhinay profile image
AnthonyJohn

This could be a python 4.x.x!

Collapse
 
paytonthemartian profile image
Payton

The language is wrote in V, perhaps you can make a transpiler for V along with C, then you have fast compile time as well.

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
 
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
 
eshu profile image
Eshu

Have you seen Scala 3?

Collapse
 
emvikrant profile image
em-vikrant

Does swallow going to support OOPS?

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

not yet

Collapse
 
otumianempire profile image
Michael Otu

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
 
chrsoo profile image
Christoffer SOOP

You might want to fix the typo for the inline assembly example... :-D

Collapse
 
ethanolchik profile image
Ethan Olchik

Where is it?

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
 
krimotemam profile image
Krimo

Is it swallow or peregrine the name of lang ? great work btw, I m pretty amazed by the time and or performance ratio for pretty much the same syntax.

Collapse
 
ethanolchik profile image
Ethan Olchik

Peregrine. We renamed the language because people were suggesting it. Sorry for any confusion, and thanks for your support!