C can do this too and it's faster than Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 10 ก.พ. 2025

ความคิดเห็น • 108

  • @stephaneduhamel7706
    @stephaneduhamel7706 9 ชั่วโมงที่ผ่านมา +97

    "Python generators in C"
    > looks inside
    > assembly

    • @jonaslamprecht9169
      @jonaslamprecht9169 9 ชั่วโมงที่ผ่านมา +8

      >looks inside further
      >machine code

    • @hoyoreverse
      @hoyoreverse 8 ชั่วโมงที่ผ่านมา

      ​@@jonaslamprecht9169 but "machine code" is literally "same" thing as assembly. These instructions but encoded in binary form instead of "readable" text. (if we throw away all the ELF/PE stuff, but still it's just a binary format)

    • @monkqp
      @monkqp 7 ชั่วโมงที่ผ่านมา +5

      ​@@hoyoreverseeh, technically correct, but assembly has a bunch of nice features straight machine code doesn't have, like labelling the shit you use

    • @hoyoreverse
      @hoyoreverse 7 ชั่วโมงที่ผ่านมา

      @@monkqp well yea, this "labelling" will be then converted to a relative jmp

    • @Capewearer
      @Capewearer 7 ชั่วโมงที่ผ่านมา +1

      @@hoyoreverse many assembly languages allow you to define macros. So there's still a difference.

  • @Apoque
    @Apoque 54 นาทีที่ผ่านมา +1

    This is so cool. This is really reminding me of when Rust was putting together the pieces for async/await. Effectively in Rust their async blocks/functions get turned into generators that return a sequence of "wakers" and then when the generator is done it yields the result at the end.

  • @amedeoalf
    @amedeoalf 12 ชั่วโมงที่ผ่านมา +42

    Feels good to arrive just in time for mista zozin upload

    • @eckhardt092
      @eckhardt092 8 ชั่วโมงที่ผ่านมา +1

      Yesu yesu yesu

    • @AirBender-s2p
      @AirBender-s2p 7 ชั่วโมงที่ผ่านมา

      fire lord sozin ?

  • @lorenzo42p
    @lorenzo42p 5 ชั่วโมงที่ผ่านมา +3

    when I was in highschool, I took every computer class that was available. I took a typing class, got good at solitaire in that class. there was a small html class available, only maybe 8 of us in the class. after learning the tag in school I was going home and learning perl.

  • @RobertFletcherOBE
    @RobertFletcherOBE 9 ชั่วโมงที่ผ่านมา +18

    StopIteration is used for control flow in most loops. Python Exceptions are not C++ exceptions and they don't have the same impact on speed. From what I understand StopIteration is also not a full fat exception, its a special case which is very efficient to raise.
    I agree it seems odd, and I don't know why they chose to use it, but it isn't as slow as folks expect when they see 'exception'

    • @khuntasaurus88
      @khuntasaurus88 8 ชั่วโมงที่ผ่านมา +3

      @@RobertFletcherOBE yes, most python exceptions derive from "Exception" class except some special cases like StopIteration and KeyboardInterrupt which detive from "BaseException" meaning they can't be caught with a regular "except Exception as e" since (maybe confusingly) Exception doesnt implement BaseException

    • @Daniel_Zhu_a6f
      @Daniel_Zhu_a6f 8 ชั่วโมงที่ผ่านมา +3

      i've ran a simple test and it does a stack unwind, which is the main cause of exceptions being slow:
      ```python
      def b(x):
      if x>= 3: raise StopIteration;
      class G:
      def __init__(self): self.x = 0
      def __iter__(self): return self
      def __next__(self):
      b(self.x)
      self.x += 1
      return self.x
      def main():
      for x in G(): print(x, end=', ') # should print `1, 2, 3,`
      main()
      ```
      but dis.dis(main) shows that it uses special bytecode instruction to catch the StopIteration.
      apart from being slow, it's also very confusing, compared to breaking the loop on a sentinel value. with all the optimizations it will apparently still go through the completely necessary stack unwind, albeit a short one, which is much slower than a normal return.

    • @cheebadigga4092
      @cheebadigga4092 6 ชั่วโมงที่ผ่านมา

      @@Daniel_Zhu_a6f you can also use for/else to "catch" that the for-block wasn't iterated at all. Different use case but sometimes it's easy to confuse the two and accidentally go the slower route (whichever that is for your use case)

    • @rogo7330
      @rogo7330 6 ชั่วโมงที่ผ่านมา +1

      @@Daniel_Zhu_a6f breaking loops on "special values" sucks - you're populating code with lots of 'if'-s that essentially just mean "go up". Even JS has something better - lables, which you can put after 'break' keyword and just break out of any block upwards, and they even namespace shadowed.

    • @thesenamesaretaken
      @thesenamesaretaken 5 ชั่วโมงที่ผ่านมา +3

      ​@@khuntasaurus88 >Exception doesn't implement BaseException
      Good old OOP

  • @danielchicoelamo
    @danielchicoelamo 10 ชั่วโมงที่ผ่านมา +6

    It is not where u study, it is how u think and solve problems, thanks for ur examples

  • @avhd187
    @avhd187 6 ชั่วโมงที่ผ่านมา +7

    C and Python goes together. There is no feud.

    • @ABaumstumpf
      @ABaumstumpf 4 ชั่วโมงที่ผ่านมา +1

      C does the work, Python dictates what works needs to be done.

    • @k1ngjulien_
      @k1ngjulien_ 3 ชั่วโมงที่ผ่านมา +2

      they are not enemies. in fact they're kissing, sloppy style

  • @chillydill4703
    @chillydill4703 8 ชั่วโมงที่ผ่านมา +3

    Mega off topic but the network series (websockets and stuff) was very interesting. Then at work today, I had to troubleshoot some network stuff with wireshark and thought to myself "what app would Tsoding create if he made an enterprise ready, sort of wireshark or proxyman app." .
    I guess it's just probably me, but that would be a cool series and very challenging for him as well :)

  • @rogo7330
    @rogo7330 6 ชั่วโมงที่ผ่านมา +2

    Returning value from the function is, essentially, the same as saving something to some "global", non-local variable. You're contracted by the C function convention that you either return nothing (void), or you return something that _can_ be returned through 'rax'. Idk, maybe not having a syntax that mandates you to put something after 'return' is actually better, because you can just pre-set 'rax', and function can just not touch it, and still you will have a benefit of having type checking of this value in your language.

  • @000dr0g
    @000dr0g 9 ชั่วโมงที่ผ่านมา +1

    Bravo! You are on a roll. What next for your context restoration plumbing? As I was writing this comment, you mentioned "God" versus the "Universe" in a philosophical moment musing over your channel's success. I recently found the philosophy that I like the best - Absurdism! Albert Camus' observation that life is absurd; it is best just to get on with enjoying the moment the best we can.

  • @mmilerngruppe
    @mmilerngruppe ชั่วโมงที่ผ่านมา

    lovely cocroutine for pison

  • @psteven5
    @psteven5 4 ชั่วโมงที่ผ่านมา +1

    C would be peak if it had short shorts, long shorts, and short longs

  • @Capewearer
    @Capewearer 7 ชั่วโมงที่ผ่านมา +4

    3:50 it's slow by design. The variables can have different precision, but not fixed width or at least machine word width. Each time you change the value you actually create the object. For loops are wrappers upon iterators, that's why they're slower than pure iterators. And so on!

  • @DuskyDaily
    @DuskyDaily 12 ชั่วโมงที่ผ่านมา +3

    Hi Mr. Zozin 😁. Hehe Lazy evaluation, sounds a good next steo after exoloring cooperative async stuff.

    • @pan-gloowl
      @pan-gloowl 12 ชั่วโมงที่ผ่านมา +7

      Mr. Azozin

  • @bartlomiejodachowski
    @bartlomiejodachowski 8 ชั่วโมงที่ผ่านมา +2

    my peasant generator would be a function using static variables xD

  • @cheebadigga4092
    @cheebadigga4092 7 ชั่วโมงที่ผ่านมา +1

    Zozin, you do know that for loops are far worse than throwing and catching exceptions (specifically StopIteration) in Python when it comes to performance? StopIteration is great if you use it more. By the way, when you start to write a py file, you already know its gonna be slow, so I don't see a problem with it in that regard at all.

  • @at-2974
    @at-2974 7 ชั่วโมงที่ผ่านมา +1

    I swear the like counter went brrr the moment he created main.py file

  • @chri-k
    @chri-k 7 ชั่วโมงที่ผ่านมา +1

    4:12 it is reminiscent enough that that in C++ it is called a coroutine
    ( C++ coroutines are quite... uhh... pls don't )

    • @purpasmart_4831
      @purpasmart_4831 ชั่วโมงที่ผ่านมา

      C++ is just a disaster in general.

  • @bernardcrnkovic3769
    @bernardcrnkovic3769 7 ชั่วโมงที่ผ่านมา

    29:40 same problem as "you need to wipe 4 times in order to know you only needed 3" 🤣

  • @Mirgeee
    @Mirgeee 7 ชั่วโมงที่ผ่านมา

    I am a simple man. I see zozin video, I click.

  • @atduyar
    @atduyar 12 ชั่วโมงที่ผ่านมา +2

    Great session btw.

  • @theodorealenas3171
    @theodorealenas3171 11 ชั่วโมงที่ผ่านมา

    I actually didn't expect that to be possible without changing the language parser.

  • @azharalibhutto1209
    @azharalibhutto1209 3 ชั่วโมงที่ผ่านมา

    Great 👍

  • @WarmNiceCozy
    @WarmNiceCozy 9 ชั่วโมงที่ผ่านมา

    I’m so excited. He programmed in python. 🎉

  • @iglobrothers645
    @iglobrothers645 11 ชั่วโมงที่ผ่านมา +2

    Are we ever going to see ZozinOS?

  • @GesteromTV
    @GesteromTV 7 ชั่วโมงที่ผ่านมา

    Jai will be GOAT when it came out

  • @matthartley6537
    @matthartley6537 3 ชั่วโมงที่ผ่านมา

    Do you have a source on C being faster than Python? Seems hard for me to believe

    • @joseoncrack
      @joseoncrack 14 นาทีที่ผ่านมา

      😂

  • @k4r4m310.
    @k4r4m310. 8 ชั่วโมงที่ผ่านมา

  • @y00t00b3r
    @y00t00b3r 11 ชั่วโมงที่ผ่านมา +3

    lol, I guess Mr. Azozin hasn't ever tried using generators in ECMAScript ?

    • @LeaoMartelo
      @LeaoMartelo 10 ชั่วโมงที่ผ่านมา +1

      Emacs script runs in the browser

    • @y00t00b3r
      @y00t00b3r 9 ชั่วโมงที่ผ่านมา +1

      @@LeaoMartelo read my post again

    • @LeaoMartelo
      @LeaoMartelo 9 ชั่วโมงที่ผ่านมา +2

      @y00t00b3r its a joke

    • @y00t00b3r
      @y00t00b3r 6 ชั่วโมงที่ผ่านมา +1

      @@LeaoMartelo YOU GOT ME !!! 😀

  • @bizarrecentral6032
    @bizarrecentral6032 12 ชั่วโมงที่ผ่านมา +4

    What development environment is this ? Looks very efficient.

    • @ReyLamurin
      @ReyLamurin 12 ชั่วโมงที่ผ่านมา +17

      Emacs

    • @hamiltonianpathondodecahed5236
      @hamiltonianpathondodecahed5236 11 ชั่วโมงที่ผ่านมา +22

      VSCode

    • @augustrum1358
      @augustrum1358 11 ชั่วโมงที่ผ่านมา +1

      man...

    • @GegoXaren
      @GegoXaren 11 ชั่วโมงที่ผ่านมา +5

      ed

    • @EnDeRBeaT
      @EnDeRBeaT 11 ชั่วโมงที่ผ่านมา +5

      pen and paper

  • @vilhola2187
    @vilhola2187 9 ชั่วโมงที่ผ่านมา

    how do you get gruber darker theme for terminal

  • @ferdynandkiepski5026
    @ferdynandkiepski5026 10 ชั่วโมงที่ผ่านมา

    Somewhat similar to std::generate in C++23.

  • @neraid
    @neraid 8 ชั่วโมงที่ผ่านมา

    $x can do this too and it's faster than Python

  • @alexmalinin2387
    @alexmalinin2387 7 ชั่วโมงที่ผ่านมา

    Showing generators via example in python and not in JS PepeHands

  • @ViniciusMiguel1988
    @ViniciusMiguel1988 9 ชั่วโมงที่ผ่านมา

    Oh no python no no

  • @elgalas
    @elgalas 10 ชั่วโมงที่ผ่านมา

    EmacsScript (JavaScript) has this too

  • @davinelulinvega
    @davinelulinvega 9 ชั่วโมงที่ผ่านมา

    It would be better for educational purposes to show generators in python console, "live mode", so to speak, since it's interpreted language

  • @ttt69420
    @ttt69420 5 ชั่วโมงที่ผ่านมา

    Isn't python just like a giant C wrapper

    • @mbarrio
      @mbarrio 3 ชั่วโมงที่ผ่านมา

      Isn't C just a big Microcode wrapper?

    • @ttt69420
      @ttt69420 2 ชั่วโมงที่ผ่านมา

      @@mbarrio I wasn't be abstract. Python is literally written in C.

  • @bagofmanytricks
    @bagofmanytricks 9 ชั่วโมงที่ผ่านมา

    This guy keeps jumping to the wrong premature conclusions like all the time

  • @namefreenargrom5694
    @namefreenargrom5694 7 ชั่วโมงที่ผ่านมา

    Do "defer" next. Is it even doable?

    • @Daniel_Zhu_a6f
      @Daniel_Zhu_a6f ชั่วโมงที่ผ่านมา

      @@namefreenargrom5694 you can emulate it with labels. but no, you can't emulate defer without a powerful macro system, since it inserts code into many places.

  • @gsestream
    @gsestream 11 ชั่วโมงที่ผ่านมา

    actual usefulness? or just fun and games. asterisk simple jail.

  • @Heater-v1.0.0
    @Heater-v1.0.0 9 ชั่วโมงที่ผ่านมา

    Python? No. I hate snakes.

    • @TheCommunistRabbit
      @TheCommunistRabbit 9 ชั่วโมงที่ผ่านมา

      We'll that's one way to say you're not gay

  • @rasulseidagul
    @rasulseidagul 7 ชั่วโมงที่ผ่านมา

    So much complexity while a “generator” is just a struct and a switch case 😂

  • @User948Z7Z-w7n
    @User948Z7Z-w7n 10 ชั่วโมงที่ผ่านมา

    JavaScript can do this too

  • @AndrewTiger
    @AndrewTiger 11 ชั่วโมงที่ผ่านมา

    bliah

  • @Sitris-h7q
    @Sitris-h7q 12 ชั่วโมงที่ผ่านมา +16

    "Programming" in Python. Its called scripting brooo!! python is not language for real ROGRAMMERS

    • @Drudge.Miller
      @Drudge.Miller 11 ชั่วโมงที่ผ่านมา +3

      Yehar Bro, real PROGRAMMERS only program in real PROGRAMMING LANGUAGES!!!

    • @GegoXaren
      @GegoXaren 11 ชั่วโมงที่ผ่านมา +1

      Oooooooo!

    • @alejandroioio6784
      @alejandroioio6784 8 ชั่วโมงที่ผ่านมา +4

      Yes!!! Real programmers only use machine language!!!! Anything else is just frontend

    • @ABaumstumpf
      @ABaumstumpf 4 ชั่วโมงที่ผ่านมา +1

      @@alejandroioio6784 Real programmers just use butterflies.

    • @alejandroioio6784
      @alejandroioio6784 4 ชั่วโมงที่ผ่านมา

      @ABaumstumpf xd

  • @porknite
    @porknite 9 ชั่วโมงที่ผ่านมา

    I think everybody knows that C can do everything Python can do but better lmao

    • @Opharg
      @Opharg 2 ชั่วโมงที่ผ่านมา

      Define better. Someone who knows python at an equivalent level is gonna have it set up and running in less time than the C programmer. Different tools, for different goals.

  • @minirop
    @minirop 10 ชั่วโมงที่ผ่านมา

    a real nobhead.

  • @paca3107
    @paca3107 8 ชั่วโมงที่ผ่านมา +1

    Thumb up if you prefer python than c