Python programmer vs C programmer (speedrun)

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 ก.ย. 2024
  • Those Data Structures & Algorithms give me nightmares...
    Join the membership to support the channel 🙃 - youtube.com/@Virbox/join
    Python programming language is perceived as slow but easy to write, and C perceived as fast, but slow to write...
    Music in the end: Mick Gordon - The Only Thing They Fear Is You (from Doom OST)

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

  • @Tekay37
    @Tekay37 ปีที่แล้ว +7578

    I like that fact you can type faster when writing in C. I think that's the best feature of that language.

    • @cdey3886
      @cdey3886 ปีที่แล้ว +278

      He uses Vi that's why

    • @doctorbobstone
      @doctorbobstone ปีที่แล้ว +233

      The C sound track is pretty good, too.

    • @oxycodin2253
      @oxycodin2253 ปีที่แล้ว +32

      @@doctorbobstone Doom OST - Meathook

    • @ennead322
      @ennead322 ปีที่แล้ว +69

      Probably a gaming chair helped as well?

    • @Oscky2013
      @Oscky2013 ปีที่แล้ว +2

      @@ennead322 not really

  • @TileBitan
    @TileBitan 2 ปีที่แล้ว +6060

    C is such a high level language... Glad i started with electric circuits applied to the radio in the 1890s

    • @gtxg.
      @gtxg. 2 ปีที่แล้ว +594

      Bro electric circuits are so high level, I manipulate the atoms inside the chips

    • @creepynutsrl
      @creepynutsrl ปีที่แล้ว +343

      @@gtxg. bro manipulating atoms inside chips is so high level, I manipulate the quarks inside the electrons protons and neutrons inside atoms inside of chips

    • @bikdigdaddy
      @bikdigdaddy ปีที่แล้ว +307

      @@creepynutsrl you guys won't understand the basics.
      I start by inventing the universe

    • @marble17
      @marble17 ปีที่แล้ว +6

      I see smiling face in your pfp

    • @eljuano28
      @eljuano28 ปีที่แล้ว

      Naw, he's got gas.

  • @dan00b8
    @dan00b8 ปีที่แล้ว +2632

    As a C programmer, i can confirm doom music is playing when we start typing

    • @NotTheHeroStudios
      @NotTheHeroStudios ปีที่แล้ว +53

      Ahh. I see why I keep having issues whole learning it, I tend to listen to softer music.
      I'll try a different Playlist

    • @scartyz762
      @scartyz762 ปีที่แล้ว +3

      @@NotTheHeroStudios So, how's it been since you switched playlists? I might try it myself.

    • @unknownguy5559
      @unknownguy5559 ปีที่แล้ว +6

      @@scartyz762 he has ascended.

    • @xenia_fox_girl
      @xenia_fox_girl 4 หลายเดือนก่อน +2

      its a scary language but i love it

  • @steveoc64
    @steveoc64 2 ปีที่แล้ว +2653

    Try in JS, and it returns the string “true”, which equates to false.

    •  2 ปีที่แล้ว +27

      In js think there are set datatype.... It's easyer ....

    • @harrytsang1501
      @harrytsang1501 ปีที่แล้ว +60

      a_set = new Set(a);
      return b.filter(elem => a_set.has(elem));
      Much more readable than python and has lower runtime complexity than the C version

    • @Israel220500
      @Israel220500 ปีที่แล้ว +89

      @@harrytsang1501 How is that more readable than it's equivalent in python?
      a_set = set()
      a_set.intesection(b)
      Or even better using proper set notation:
      a = {1,2,3,5}
      b = {2,4,5,8}
      print(a & b) # prints {2,5}

    • @zm5856
      @zm5856 ปีที่แล้ว +3

      @@Israel220500 👍

    • @prodbytukoo
      @prodbytukoo ปีที่แล้ว +8

      @@Israel220500 Admit it, dude's solution is much more readable.
      Set datatypes are way superior for this type of discrete problems.

  • @onee
    @onee 2 ปีที่แล้ว +3431

    I also write C in 4x speed compared to Python for some odd reason.

    • @zm5856
      @zm5856 ปีที่แล้ว +23

      🤓

    • @Cypekeh
      @Cypekeh ปีที่แล้ว +78

      probably vim

    • @MasterAdrenalyn
      @MasterAdrenalyn ปีที่แล้ว +70

      everytime i go back to python i have to readapt myself to not using brackets

    • @abdelidrissi7241
      @abdelidrissi7241 ปีที่แล้ว +29

      c has lot of useless stuff you need to type, python you have to think about everyline

    • @bforbiggy
      @bforbiggy ปีที่แล้ว +68

      @@abdelidrissi7241 I can't tell if this is a joke

  • @cookie_space
    @cookie_space 4 หลายเดือนก่อน +19

    CS major here:
    Python is an interpreted language. That means the keyboard has to do extra work before the keystrokes can be send to the PC. That's why it is usually faster to type in C.

  • @vaulttectradingco8438
    @vaulttectradingco8438 2 ปีที่แล้ว +420

    Python dev would have done list(set(a) & set(b))

    • @homerlol9058
      @homerlol9058 ปีที่แล้ว +112

      Stop letting the world know we don't know how to code

    • @nathanaeltrimm2720
      @nathanaeltrimm2720 ปีที่แล้ว +25

      I’m just learning python and thought that would’ve been the solution, then when he started writing a far more robust code than I was prepared for I then thought to myself “ahh yes, I must have overlooked something - how foolish of me”

    • @eddieisfiction442
      @eddieisfiction442 ปีที่แล้ว +7

      while true, technically a list is a collection, not a true array. You got to import array and do it the hard way.

    • @TheMrCarnification
      @TheMrCarnification ปีที่แล้ว +35

      @@nathanaeltrimm2720 the video has overlooked duplicate values. The set approach is more consistent. It's slow af but who cares we are using python anyway

    • @raskr8137
      @raskr8137 ปีที่แล้ว +12

      @@TheMrCarnification set() is slow but isn't a list comprehension even slower in python?

  • @bayzed
    @bayzed ปีที่แล้ว +789

    Your C code is O(n*m). You could sort the arrays first and then use two pointers to compare the sorted arrays and check for intersection. This would drop it down to O(n*logn + m*logm + m + n) which simplifies to O(n*log(n) + m*log(m)). This assumes an efficient sort algo like mergesort that offers O(xlogx)-. In case n>m, the final time complexity is O(n*log(n)).

    • @bayzed
      @bayzed ปีที่แล้ว +128

      Also, the C code being more explicit makes it clearer here what is happening in my opinion. The list comprehension + 'in' in Python hides how efficient this approach is. Is the interpreter smart enough to convert b into a hashset while checking to avoid slow linear search?

    • @Virbox
      @Virbox  ปีที่แล้ว +206

      I don't think Python interpreter works this way - 1st code is O(n*m) too

    • @prodbytukoo
      @prodbytukoo ปีที่แล้ว +85

      @@bayzed in my eyes python solution was much less readable than C solution, but maybe that's because I'm not that much into python as I'm with C/C++

    • @Kaeribana
      @Kaeribana ปีที่แล้ว +189

      @@prodbytukoo wdym, the python solution was very easy to read and pretty logical too

    • @Gandarf_
      @Gandarf_ ปีที่แล้ว +54

      Or just use hashmap....

  • @EarlyBitcoiner
    @EarlyBitcoiner 2 ปีที่แล้ว +1225

    The soundtrack for each language gives very accurate feeling about the skill level of each programmer. C programmers are superior of course

    • @svlmain
      @svlmain ปีที่แล้ว +140

      as someone who knows both, i am not superior to anyone i am extremely stupid.

    • @ignacio1085
      @ignacio1085 ปีที่แล้ว +22

      @@svlmain u ok, bro?

    • @quack3891
      @quack3891 ปีที่แล้ว +51

      @@ignacio1085 he's having an existential crisis

    • @adil080_
      @adil080_ ปีที่แล้ว +20

      Sir I respect you opinion but sir why would I just not do it the easy way. Why would I painful write 50 lines if I can just write 5. All and all that's my opinion :)

    • @quack3891
      @quack3891 ปีที่แล้ว +49

      @@adil080_ the same reason if I would want my code to run 42x faster with the 5 lines than 50. C++ and C are meant for performance, python might be easier, but its to be noted that it's also slow as hell

  • @ademyro
    @ademyro ปีที่แล้ว +681

    I like the fact that he uses vi just for C

    • @macreator9497
      @macreator9497 ปีที่แล้ว +16

      Using vi is enough

    • @Pikopati
      @Pikopati ปีที่แล้ว +28

      I like the fact he can close it

    • @Mmnc-bv3rk
      @Mmnc-bv3rk ปีที่แล้ว +41

      Not even vim, just vi...

    • @edward8064
      @edward8064 ปีที่แล้ว +7

      @@Mmnc-bv3rk i think he used nvim by looking at all of those plugins and the line numbering.

    • @anstropleuton
      @anstropleuton ปีที่แล้ว +11

      @@edward8064 Nah, 'vi' command is mostly syslink to 'vim' in many linux distros

  • @St418_
    @St418_ ปีที่แล้ว +183

    In python you can simply do print(*c).
    No need to use join and map.

    • @davidu9904
      @davidu9904 ปีที่แล้ว +23

      Just wrote it myself to confirm. Same exact output so thanks

    • @Meridian-lk2fo
      @Meridian-lk2fo ปีที่แล้ว +5

      Woah

    • @mousepotatoliteratureclub
      @mousepotatoliteratureclub ปีที่แล้ว +3

      That's going to be really useful to me. Thank you!

    • @stackercoding2054
      @stackercoding2054 ปีที่แล้ว +4

      that's right, but what I dont get is the "map" function there, why parse each array item to string before showing on screen? it would just show the same thing

    • @St418_
      @St418_ ปีที่แล้ว +6

      @@stackercoding2054join can only accept Iterable[str] as parameter, it's not possible to directly join int.

  • @Elfcheg
    @Elfcheg ปีที่แล้ว +148

    C is very user friendly. He's just choosy about his friends.

  • @leu9455
    @leu9455 ปีที่แล้ว +121

    You could write print(*c) instead of print(" ".join(map(str,c)))

    • @dingding4898
      @dingding4898 ปีที่แล้ว +7

      Who are you, & Why're you so smart in the ways of python?
      What is this way called?

    • @blitzpark1250
      @blitzpark1250 ปีที่แล้ว +10

      @@dingding4898 hes god

    • @leu9455
      @leu9455 ปีที่แล้ว +1

      ​@@dingding4898 Hi!
      It's called the unpack operator.
      It's a very useful operator that can be useful in other contexts.
      For example:
      You can write [*...] instead of list(...)
      Or {*...} instead of set(...)
      But the unpack operator can be useful in a lot of other contexts.
      For example, if you want the first and last element, you will usually do something like:
      a,b=a_list[0], a_list[-1]
      But actually, you can write it in a shorter way with the unpack operator:
      a,*_,b=a_list
      And yes it works
      Python is a very rich and beautiful language.
      If you want to learn some tricks like that, you can read the documentation. But there is also a branch of programming which is called code golf, which consists in doing the smallest code possible. And so, you often learn new tricks. I created my own website about code golf, its called WeekGolf (week.golf) but there are other good websites like code golf stack exchange.
      Hope it helps :)

    • @tomasadams3922
      @tomasadams3922 ปีที่แล้ว +5

      @@dingding4898 i think it is called unpacking

    • @dingding4898
      @dingding4898 ปีที่แล้ว

      But why does *c does join and map at once?

  • @МаксимКайтмазян-л1ю
    @МаксимКайтмазян-л1ю ปีที่แล้ว +36

    And the program in C, of course, has an error in working with memory.

    • @linuxization4205
      @linuxization4205 ปีที่แล้ว

      The compile time of python is sprinkled into the runtime speed

    • @weatherman1504
      @weatherman1504 ปีที่แล้ว

      @@linuxization4205 What does Python's combo runtime and compile time have to do with writing borked memory management into C code?

    • @user-hk3ej4hk7m
      @user-hk3ej4hk7m 4 หลายเดือนก่อน +4

      It is indeed very fast at segfaulting

    • @notjebkerman6207
      @notjebkerman6207 3 หลายเดือนก่อน

      You mean allocing without free? I'll admit that it's bad, but it's not an error because the program ends quickly.
      When writing missile guidance software, their approach to freeing memory is "just wait until the ram is destroyed".
      Personally, I would have used alloca() to allocate an "out" buffer on the stack before passing it to intersect().

  • @Eknoma
    @Eknoma ปีที่แล้ว +166

    Your C code and Python code work differently if there can be duplicates in the arrays.
    If there are `n` of the same in a, and `m>0` of the same in b, then your Python code will give `n` duplicates, while your C code will give `n × m` duplicates, which might mean you go into unallocated memory
    This flaw in the C code could be very easily fixed by adding a `break;` after finding the first bi such that `a.arr[ai] == b.arr[bi]`. (i.e. after line 18)

    • @inviatrroxdx6118
      @inviatrroxdx6118 ปีที่แล้ว

      fuck of brother, let us be stupid

    • @nkoninn
      @nkoninn ปีที่แล้ว +3

      Your comment just reminded me about this video: th-cam.com/video/oTEiQx88B2U/w-d-xo.html 😂

  • @Pokedollar
    @Pokedollar ปีที่แล้ว +227

    Every time I see calloc I remember why I suffer from depression

    • @radmir_khusnutdinov
      @radmir_khusnutdinov ปีที่แล้ว +37

      And the memory hasn't been freed! That kills me.

    • @RurikLoderr
      @RurikLoderr ปีที่แล้ว +44

      @@radmir_khusnutdinov Just restart the computer... that frees everything.

    • @Bl0xxy
      @Bl0xxy 4 หลายเดือนก่อน +2

      @@RurikLoderr windows handles memory y'all, when the exe is done running windows will take its memory back

    • @IamPyu-v
      @IamPyu-v 4 หลายเดือนก่อน +1

      @@Bl0xxy He's not using Windows

    • @Bl0xxy
      @Bl0xxy 4 หลายเดือนก่อน

      @@IamPyu-v unix doesn't do that?

  • @BassHero55
    @BassHero55 ปีที่แล้ว +77

    Impressive. Very nice. Let's see a Rust developer's speedrun.

    • @georgegriffin6114
      @georgegriffin6114 ปีที่แล้ว +29

      They are still trying to satisfy the borrow checker

    • @butterybread4162
      @butterybread4162 ปีที่แล้ว +11

      The tasteful indentation of it. Oh my god, it even has a docstring.

    • @KohuGaly
      @KohuGaly ปีที่แล้ว +12

      a.into_iter().flat_map(|x| b.contains(x).then_some(x)).collect::()
      Honestly, it's just like the python but with weirder more verbose syntax.

    • @weatherman1504
      @weatherman1504 ปีที่แล้ว +1

      *Rust, Dust and Guts starts playing*

    • @김인영-q5x
      @김인영-q5x 3 หลายเดือนก่อน

      why do you have emojis?

  • @DaJodad
    @DaJodad ปีที่แล้ว +34

    And remember, just because it's written in C, doesn't mean you programmed it to run faster.

    • @keris3920
      @keris3920 ปีที่แล้ว +6

      Yes it does. Python is interpreted, c is compiled. $20 says the c implementation is an order of magnitude or more faster.

    • @DaJodad
      @DaJodad ปีที่แล้ว +4

      @@keris3920 I said "doesn't mean you" programmed it to run faster. C often allows for some pretty dangerous code and can easily become very inefficient even when solving the same problem. Of course with the best implementation it's going to run faster.

    • @keris3920
      @keris3920 ปีที่แล้ว +6

      @@DaJodad Sure, but you can write some really bad code in Python as well. A simple matrix multiplication in Python using for loops is more than 20,000x slower than the C implementation, and both of those implementations are O(n^3).
      My point is, the argument you're trying to make is not an argument for using Python. Writing the code in C, contrary to what you just said, is almost guaranteed to make your program much faster. As you mentioned, C can be dangerous because it's close to the hardware. This is the real reason someone should weigh when choosing the language. Sometimes python is inappropriate, and sometimes C is too cumbersome for the scope of your problem. But good C code will always be faster than good python code. Heck, even poor c code is often faster than good python code.
      Careful with the safety argument too, by the way. C has a compiler, which means it can catch a ton of bugs at compile-time. Python is interpreted and relies on runtime tests, which does make Python more dangerous to deploy to customers. Again, there are tradeoffs in all languages.

    • @linuxization4205
      @linuxization4205 ปีที่แล้ว

      The compile time of python is sprinkled into the runtime speed

    • @keris3920
      @keris3920 ปีที่แล้ว

      @@linuxization4205 you can't accidentally deploy code that doesn't compile.

  • @electra_
    @electra_ ปีที่แล้ว +74

    most accurate part was that the C programmer coded in VI
    but the C programmer should have just ended on getting a segfault lmao

  • @mekatronikachmadi5472
    @mekatronikachmadi5472 2 ปีที่แล้ว +51

    This is weird,, since most Python interpreters actually written in C

  • @mrmaniac9905
    @mrmaniac9905 ปีที่แล้ว +82

    You know this is not the most efficient way right? you're doing in O(N * N) when you could do it in O(N). You would just put one of the arrays into a hashset, then loop through the array, every iteration for collision into the hashset, because the hashset is constant time lookup, assuming you are passing in values that have hashes, you are able to do this in O(N) in any language.

    • @Virbox
      @Virbox  ปีที่แล้ว +26

      So intersect function body should be replaced with [i for i in a if i in set(b)] for maximum efficiency?

    • @mrmaniac9905
      @mrmaniac9905 ปีที่แล้ว +10

      @@Virbox Honestly, I don't know python but that's the general idea.

    • @zm5856
      @zm5856 ปีที่แล้ว +5

      @@mrmaniac9905 🤓

    • @buffcode
      @buffcode ปีที่แล้ว

      @@zm5856 Are you really trying to call someone a nerd while browsing programming humour? You're in OUR territory, you fool!

    • @zm5856
      @zm5856 ปีที่แล้ว +1

      @@buffcode new phone who dis?

  • @donno2048
    @donno2048 ปีที่แล้ว +38

    In python you could just `c=set(a) & set(b)` which also removes duplicates

    • @protectedmethod9724
      @protectedmethod9724 ปีที่แล้ว +1

      Not if multiple values are the same

    • @itellyouforfree7238
      @itellyouforfree7238 ปีที่แล้ว +1

      @@protectedmethod9724 what are you talking about?

    • @abyrvalg_
      @abyrvalg_ ปีที่แล้ว

      @@protectedmethod9724 you need to find intersected values, which means you need set of those values.

    • @protectedmethod9724
      @protectedmethod9724 ปีที่แล้ว +2

      @@abyrvalg_ problem statement was intersection of two arrays not two sets. so the question is should repeated values be left in the result or removed? it's undefined

    • @Hardcore_Remixer
      @Hardcore_Remixer ปีที่แล้ว +1

      Then in C one should apply qsort on both arrays and just then check for intersection (duplicates are easy to ignore in sorted arrays).
      That if you want the same time complexity.

  • @warrenhenning8064
    @warrenhenning8064 2 ปีที่แล้ว +41

    def intersect(a, b): return set(a).intersection(set(b))
    There's no need to free up the memory the C code allocated in this case since it will just be freed by the OS when the program terminates. It'd only be a memory leak if the program continued running without the memory ever being cleaned up, e.g. if you ran the intersect function multiple times and none of the allocations were freed.

    • @blueghost3649
      @blueghost3649 ปีที่แล้ว

      @Artify cleaning up memory is a very slow unnecessary process

    • @GeorgeFosberry
      @GeorgeFosberry ปีที่แล้ว +11

      Unfortunately there are geniuses like you who do not free memory even in production code because "the OS will free it anyway". Libfontconfig comes to mind, where they allocate memory and store the pointer in global static variable, never freeing it. Now everyone need to add this allocation to whitelist of their Valgrind/ASan/whatever.

    • @blueghost3649
      @blueghost3649 ปีที่แล้ว +1

      @@GeorgeFosberry freeing up memory can be pointless, if you don't plan on running a program for too long/for a one time task, then it is pointless

    • @GeorgeFosberry
      @GeorgeFosberry ปีที่แล้ว +9

      @@blueghost3649 It seems that you didn't pay attention while reading my comment. Unless you wrote some one-off script and then threw in to garbage bin, chances are that your piece of code will be used as a part of a bigger system. A piece of code in isolation can't know if it is allowed to cut corners by leaking owned resources - and that means it should always properly release those resources.

    • @quack3891
      @quack3891 ปีที่แล้ว +5

      @@blueghost3649 it might be pointless, but it's incredibly important for large corporate code. If the user who's using the abstracted code doesn't realize that there's a memory leak further up the chain and then runs multiple children using that memory leak or exists for the duration of the program (which could take a while), or isn't seen and closed by the system during the duration, then the problem is now no longer negligible

  • @notfinxx
    @notfinxx ปีที่แล้ว +2

    The music at the beginning with the really high sounds is from The Escapists OST. It is called Lockdown

  • @thegreatken2073
    @thegreatken2073 ปีที่แล้ว +67

    Alright that’s write speed now let’s talk about compile and execution speed 😳

    • @ninstagram
      @ninstagram ปีที่แล้ว +47

      slightly more than 0 seconds for both

    • @Hardcore_Remixer
      @Hardcore_Remixer ปีที่แล้ว +1

      @@ninstagram For 5000 elements in each array?

    • @БогданЗинченко-к5п
      @БогданЗинченко-к5п ปีที่แล้ว +2

      @@Hardcore_Remixer still 0 seconds

    • @k1_26h
      @k1_26h 3 หลายเดือนก่อน

      ​@@Hardcore_Remixer 0.1 sec

  • @dariuschitu3254
    @dariuschitu3254 ปีที่แล้ว +47

    The C code is actually partially wrong...
    For example, arr1[] = { 8,3,1,5 }; arr2[] = { 5,3,4,6,9,3 }; returns 3 3 5
    The idea is, if the element 3 appears once in arr1[], and the element 3 appears twice in arr2[], once your FOR loop finds a correspondent for an element in arr1[], it can find many more printing it repeatedly.
    So to fix this, once you find an element in intarr b, then leave the FOR loop searching for a correspondent, because you found one - so add a break; statement inside the IF statement.
    That's just to show how easy it is to write bugs in C code

    • @itellyouforfree7238
      @itellyouforfree7238 ปีที่แล้ว +14

      the python code has the exact same bug. also, they both use O(m*n) algo, which is not optimal

    • @jhonatasbonfim4917
      @jhonatasbonfim4917 ปีที่แล้ว +1

      @@itellyouforfree7238 o que é esse algoritmo? Por favor eu preciso saber.

    • @itellyouforfree7238
      @itellyouforfree7238 ปีที่แล้ว +3

      @@jhonatasbonfim4917 you can sort the lists in O(n log n), then compare them in order in O(n), leading to a total of O(n log n). For large n this is much better than O(n^2)

    • @simoneelia9580
      @simoneelia9580 ปีที่แล้ว

      But if you have arr1[] = {5, 3, 4, 6, 9, 3} and arr2[] = {8, 3, 1, 5} the result is wrong too, also if we use a flag to break the for loop... We have to save also the indexes of the used values...

    • @mushyomens6885
      @mushyomens6885 ปีที่แล้ว

      it's also easier to point out bugs unlike python

  • @thediabolo4421
    @thediabolo4421 2 ปีที่แล้ว +30

    After 7min already on reddit lol

  • @zurrutikGames
    @zurrutikGames ปีที่แล้ว +4

    In C++ it's super easy:
    using namespace std;
    vector a = { 8, 3, 1, 5 };
    vector b = { 5, 3, 4, 6, 9 };
    vector intersection;
    ranges::sort( a );
    ranges::sort( b );
    ranges::set_intersection( a, b, back_inserter( intersection ) );
    for( auto n : intersection )
    cout

    • @hassaniq0777
      @hassaniq0777 19 วันที่ผ่านมา

      the hell is:
      vector a = { 8, 3, 1, 5 };
      vector b = { 5, 3, 4, 6, 9 };
      i think you mrant
      vector a = { 8, 3, 1, 5 };
      vector b = { 5, 3, 4, 6, 9 };

  • @trixion74
    @trixion74 2 ปีที่แล้ว +15

    The video has only 2k views, but its actually hilarious

  • @jptmane
    @jptmane ปีที่แล้ว +35

    Cool. Let’s see the runtime performance on large arrays now

    • @alexnaosei763
      @alexnaosei763 ปีที่แล้ว +3

      🤓

    • @AR-yd2nd
      @AR-yd2nd 4 หลายเดือนก่อน

      Isn't numpy written in C under the hood?

    • @volbla
      @volbla 3 หลายเดือนก่อน

      ​@@AR-yd2nd All of C-python (the most common python implementation) is written in C. The difference is that python is meant to be general purpose and easy to use, whereas numpy is optimized for processing large arrays of numbers. That comes with certain restrictions/responsibilities on behalf of the programmer.
      For example, python integers have "arbitrary precision" to avoid overflowing. If a number becomes so large that it doesn't fit in the current datatype, python will automatically change the datatype to accomodate the new number. Numpy doesn't do that. Numpy arrays must contain one specific datatype (for example 64 bit integers). This means overflow is possible, but it also means numpy can implement certain optimizations by knowing the starting and ending datatype of all operations.
      Python lists are also optimized for dynamic sizing. They are good at updating how much memory they require when you add or remove elements. I don't believe numpy does that. Since the point of numpy is parallel computing, you're expected to create large arrays with a known size and then perform computations with them. You're not supposed to add new data to existing arrays.
      In fact, since numpy specializes in computations with large sets of data, it can actually be _slower_ than regular python if you're using very small arrays (or individual numpy numbers). It's important to know which tool is best for the job, and the best way to find that out is to test the execution time of different versions of the same code 🙂

    • @farukyldrm8498
      @farukyldrm8498 3 หลายเดือนก่อน

      ​@@AR-yd2ndis numpy also use multithreading?

    • @sergeydostovalov6180
      @sergeydostovalov6180 3 หลายเดือนก่อน

      @@farukyldrm8498 parts of NumPy are built on top of a standard API for linear algebra operations called BLAS and BLAS libraries make use of multiple threads to speed up some operations by default.

  • @someguyusingyournetflixaccount
    @someguyusingyournetflixaccount ปีที่แล้ว +4

    As someone who’s just started learning C, I’m gonna assume I unlock the inbuilt doom soundtrack in when I’ve learnt how to use 1 if statement without using stackoverflow

    • @KohuGaly
      @KohuGaly ปีที่แล้ว

      The doom soundtrack unlocks when you write your first 1000+LOC program without UB in it.

    • @notjebkerman6207
      @notjebkerman6207 3 หลายเดือนก่อน

      ​@@KohuGaly Does Implementation-defined UB (such as signed overflow with -fwrapv) still count as UB?

  • @govindsanal3092
    @govindsanal3092 2 ปีที่แล้ว +8

    You know shits about to get real when he types v

  • @Hvleos
    @Hvleos 3 หลายเดือนก่อน

    Im not a programmer im just learning python but I can say that I appriciate C. Python feels like you're driving a nice car and C feels like you understand how the car was built and can adjust every bit of it for whatever the road calls for.

  • @mech653
    @mech653 ปีที่แล้ว +7

    No wonder C is a popular language. Not only it makes you type faster, but it also plays doom music!

  • @arturmg2068
    @arturmg2068 10 หลายเดือนก่อน +4

    Now, the knowledge of who use C is so much higher than who code in Python

    • @EaZea
      @EaZea 3 หลายเดือนก่อน +1

      i code in binary so i'm the smartest man alive then

    • @arturmg2068
      @arturmg2068 2 หลายเดือนก่อน

      @@EaZea i disagree with my comment, youre right. what i said dont make sense, i gonna delete it.

  • @cmknoll3
    @cmknoll3 4 หลายเดือนก่อน

    When you realize you could have just written "return list(set(a) & set(b))" instead of that pointless list comprehension in python to do the exact same thing.

  • @stomah9832
    @stomah9832 2 ปีที่แล้ว +8

    remember to mark your return types const just in case

  • @emilmoroder2163
    @emilmoroder2163 ปีที่แล้ว

    def inter(a,b):
    return set(a) & set(b)
    # Does not remove duplicates (you could create a remove duplicate function)
    def interNaive(a,b):
    result = []
    for i in a:
    for j in b:
    if (i == j):
    result.append(i)
    return result
    a = [ 8, 3, 1, 5]
    b = [ 5, 3, 4, 6, 9]
    print(f"The intersection is: {inter(a,b)}")
    print(f"The intersection is: {interNaive(a,b)}")

  • @Un_revealing
    @Un_revealing 3 หลายเดือนก่อน +1

    Syntax: Python>>>C
    Speed: C>>>>>>Python
    BTW I am python programmer

  • @danels7
    @danels7 3 หลายเดือนก่อน

    For anyone wondering, the timer for the C clip was in days, not seconds

  • @burnin8orable
    @burnin8orable ปีที่แล้ว +1

    Write python libraries in C, and then use Python for higher level tasks.

    • @rc8s
      @rc8s ปีที่แล้ว +1

      Or just use Lua with C

  • @DMSBrian24
    @DMSBrian24 ปีที่แล้ว +5

    In python isn't converting to a set better, since that eliminates duplicates (and not to mention, comes with an intersection method)

    • @vaolin1703
      @vaolin1703 2 หลายเดือนก่อน

      Also has better runtime complexity

  • @ymts_vladyslav3707
    @ymts_vladyslav3707 ปีที่แล้ว +11

    Python developer:
    - Wrote code in 10 seconds
    - Waits 1 minute for execution
    C developer:
    - Wrote code in 30 seconds
    - Waits 1 second for execution

    • @mitchell7692
      @mitchell7692 ปีที่แล้ว +1

      But python doesn't even have to compile you can run it immediately.

    • @ymts_vladyslav3707
      @ymts_vladyslav3707 ปีที่แล้ว +1

      @@mitchell7692 I meant not compilation, because python is interpretable, but execution speed of code itself on highly iterative operations. There`s ton of memes on this topic 1 second C vs 1 minute python especially on sorting

  • @ne_uchastvuyu_v_konkurse_nikov
    @ne_uchastvuyu_v_konkurse_nikov ปีที่แล้ว +48

    Now make arrays large and include run time in comparison ;)

  • @thenasiudk1337
    @thenasiudk1337 3 หลายเดือนก่อน

    I'm a CS student and i still remembered that C is the second thing we learned after C++, don't understand shit and yet still wrote 100+ line of code for group project

  • @gelcendaa
    @gelcendaa ปีที่แล้ว +11

    Really nice and helpful... Thanks!

  • @adlzinn
    @adlzinn ปีที่แล้ว +1

    For those who dont understand why C was written faster than python:
    Actually it was a bot (programmed by the language of the vid's moment) that coded those files, and how we all know that C is faster than python, the C code was done faster.
    underrated vid

  • @wtfdoiputhere
    @wtfdoiputhere ปีที่แล้ว +10

    Pulling up vim to write C code is the equivalent of showing your abs to the ladies

  • @stoychorusinov5519
    @stoychorusinov5519 4 หลายเดือนก่อน

    My first class in coding was in C++ which I failed to learn. Then my second language that I’m still learning was R. The memories from C++ still hound me. I’m the only one in the class that uses for statements in R and python and the only that defines functions

  • @loganreidy7055
    @loganreidy7055 ปีที่แล้ว +2

    I've only ever used python not sure why I clicked on this video

  • @sohigh7433
    @sohigh7433 4 หลายเดือนก่อน +1

    You just nested two loops big man

  • @JSzitas
    @JSzitas ปีที่แล้ว

    And this is why we have C++:
    #include
    #include
    template std::vector intersect( std::vector &a,
    std::vector &b) {
    std::unordered_set comp_set(a.begin(), a.end());
    std::vector result;
    result.reserve(min(a.size(), b.size()));
    for( auto&val:b ) {
    if( comp_set.count(val) ) {
    result.push_back(val);
    }
    }
    return result;
    }
    Look how easy this is, and it's about as fast as you can get without implementing your own hashing function (afaik).

    • @pepehimovic3135
      @pepehimovic3135 ปีที่แล้ว

      “typename T”? In Java it’s just class {}

    • @JSzitas
      @JSzitas ปีที่แล้ว

      @@pepehimovic3135 Yeah, but that's Java. In C++, you use typename T is for actual types, whereas you would specify class T (so template T intersect(T&a, T&b); for a class (so not a primitive type).
      You do not have this distinction in Java because Java boxes everything (so everything is an object, even primitive types), adding unnecessary overhead, whereas C++ can work with primitive types without boxing.
      Which tends to be pretty good for performance.

  • @bartomiejlandoch5238
    @bartomiejlandoch5238 ปีที่แล้ว +3

    Ok. But python do the same job (or even more time consuming) on the lowest software layer. Python just have dedicated library to do this job. In C you need to write code which do the same, but on the lowest layer. It depends on destination of your functionality. If you need low-cost time consuming (c) or just do this job regardless costs (Python).

    • @whatusernameis5295
      @whatusernameis5295 ปีที่แล้ว +1

      aka use python for prototyping then (if you're running into performance issues anyway) remake it in C

  • @gordonzar992
    @gordonzar992 2 ปีที่แล้ว +7

    some are more equal than others

  • @michaelatlas2341
    @michaelatlas2341 ปีที่แล้ว

    You forgot the equation for the moon landing with a rocket fueled broomstick in C, but that's ok we can publish it with a minor bug no problem

  • @trapkat8213
    @trapkat8213 3 หลายเดือนก่อน

    Now try to run the two pieces of code on two arrays that each contain one million elements. In that case C is roughly 100 times quicker than Python.

  • @EzOddz
    @EzOddz 29 วันที่ผ่านมา

    Instructions unclear, accidently created an operating system

  • @needywallaby2030
    @needywallaby2030 3 หลายเดือนก่อน

    Actually it's not the speed of the C program but the fact that it makes time flow faster

  • @Yutaro-Yoshii
    @Yutaro-Yoshii ปีที่แล้ว +55

    use a hashmap. with the current impl it's O(n^2) since it contains nested loop. So unfortunately it would be slower than the python one when the input is large.

    • @okkoheinio5139
      @okkoheinio5139 ปีที่แล้ว +1

      O(n*m)
      also if "i in b" is O(n) then it will be the same

    • @Yutaro-Yoshii
      @Yutaro-Yoshii ปีที่แล้ว +3

      @@okkoheinio5139 By "n" I meant the total size of input. O(n^2) still stands here because big O denotes the worst case performance. Best case runtime happens when one of the inputs has size 1, which will take a linear (n-1) number of iterations to complete. The worst case happens when both of the input are of the same size, which would give a run time of 1/4(n^2) iterations. Thus the performance of this algorithm is O(n^2) after removing the coefficient from the worst case run time.

    • @okkoheinio5139
      @okkoheinio5139 ปีที่แล้ว

      @@Yutaro-Yoshii isn't that what it means ot be O(n*m)?

    • @Yutaro-Yoshii
      @Yutaro-Yoshii ปีที่แล้ว

      ​@@okkoheinio5139 If you set n and m to be the size of each arrays the complexity would be O(n*m), and if you set n to be the sum of the two array sizes, the complexity would be O(n^2). The result depends on how you define the input size. I should have been clear that I meant the latter case.

    • @okkoheinio5139
      @okkoheinio5139 ปีที่แล้ว

      @@Yutaro-Yoshii wouldn't your O(n^2) be in my n,m case O((n+m)^2)? since your n is the sum of my n and m
      O((n+m)^2) = O(n*m)?

  • @polychad
    @polychad 3 หลายเดือนก่อน

    The main question here is "why?"
    A program like this clearly doesn't benefit from the optimisation C provides, nor is it limited to a specific, constraining environment.
    If you know python and C, use python for this.
    Languages are tools.
    If you know how to drive and walk, maybe drive that 500 mile trip instead.

  • @Alefast51
    @Alefast51 3 หลายเดือนก่อน

    Oh no this old static strongly typed and compiled programming language requiring more code to write than a dynamic and interpreted programming language! This is madness!

  • @theloniousMac
    @theloniousMac ปีที่แล้ว

    openAI Python in 9 seconds:
    def intersection(arr1, arr2):
    # Find the intersection of the two lists
    result = list(set(arr1) & set(arr2))
    return result
    arr1 = [3, 7, 1, 9, 2]
    arr2 = [4, 1, 9, 5]
    result = intersection(arr1, arr2)
    print(result) # Output: [1, 9]

  • @呀咧呀咧
    @呀咧呀咧 ปีที่แล้ว

    Python programmers “speedrun” away when seeing c programmers

  • @niklkelbon3662
    @niklkelbon3662 ปีที่แล้ว

    C++
    using namespace std::ranges;
    auto intersect(range auto&& a, range auto&& b, auto out) {
    std::unordered_set s1(begin(a), end(a));
    std::ranges::copy_if(b, out, [&](auto& v) { return s1.contains(v); });
    return out;
    }
    int main() {
    std::vector a{1, 2, 3, 4, 5};
    std::vector b{3, 16, 2, 1, 0, -5};
    intersect(a, b, std::ostream_iterator(std::cout));
    }

  • @mossss321
    @mossss321 3 หลายเดือนก่อน

    Now I know doom slayer uses which prog language

  • @AndrewTSq
    @AndrewTSq ปีที่แล้ว

    Javascript console.log(a.filter(arr=>b.includes(arr)))

  • @StarsOfMinecrafttr
    @StarsOfMinecrafttr 4 หลายเดือนก่อน

    coding this in rust takes me 30 seconds max, and the performance is 500nanoseconds, which is 1/2000 of a millisecond, so i think its pretty fast than python, and easy to code as well.

  • @АндрейФейгельман-ш4т
    @АндрейФейгельман-ш4т ปีที่แล้ว

    Python: set(a) & set(b)
    Time complexity: O(n+m)
    ez

  • @noturgokuxd
    @noturgokuxd 6 หลายเดือนก่อน

    and those who say don't use python use c/c++ as its faster i have an advice for u. Why the heck would i write like 500 lines instead of 5? c/c++ is good too but its about someone's personal preference.

  • @rosyidharyadi7871
    @rosyidharyadi7871 ปีที่แล้ว +6

    in python you can just use builtin method: set(a).intersection(set(b))

    • @eddieisfiction442
      @eddieisfiction442 ปีที่แล้ว +2

      that’s what i was going to say. plus he never used arrays

  • @freeshavaacadooo1095
    @freeshavaacadooo1095 ปีที่แล้ว +21

    You don't gotta loop twice, you'd have O(n*m) roughly O(n^2) complexity. You could instead create a hashing function and make a hashtable and run through both arrays once, checking whether it appears in the hash table, showing that they are the same values (resolving collisions would also have to be incorporated). This would give you O(n + m) roughly O(n) complexity at the disadvantage of space complexity which would be O(n + m) roughly O(n) rather than O(1), but sacrificing space for speed is pretty worthwhile.

    • @rutviksaptarshi745
      @rutviksaptarshi745 ปีที่แล้ว +4

      A hashtable, in C????😰😰😰

    • @freeshavaacadooo1095
      @freeshavaacadooo1095 ปีที่แล้ว +4

      @@rutviksaptarshi745 easy way to make hashing functions in c using bit manipulation

    • @rutviksaptarshi745
      @rutviksaptarshi745 ปีที่แล้ว +4

      @@freeshavaacadooo1095 I mean you're right - implementing a hash table is quite doable. But the amount of code you'd need to write to make it a) be efficient b) work with different types is cray cray

    • @bunnycode3852
      @bunnycode3852 ปีที่แล้ว +1

      @@rutviksaptarshi745 unordered map

    • @rutviksaptarshi745
      @rutviksaptarshi745 ปีที่แล้ว +1

      @@bunnycode3852 I'm pretty sure there's a bunch of header only representations for these things

  • @bps745
    @bps745 ปีที่แล้ว +2

    Ok so C is faster I guess

  • @AsuCristo
    @AsuCristo 4 หลายเดือนก่อน

    As a C programmer I can tell the program is not good enough, it did not have 3 segfaults and memory leaks during the process.

  • @acidiclight
    @acidiclight ปีที่แล้ว

    I'd rather be the C programmer, it looks more fun banging that out

  • @athiradha262
    @athiradha262 ปีที่แล้ว

    1st time i've been this scared of curly brackets

  • @The_moonlit
    @The_moonlit 3 หลายเดือนก่อน

    Whos gunna tell them python has built innintersection function

  • @cyanuranus6456
    @cyanuranus6456 ปีที่แล้ว

    Just like Jython. Jython is a Python Programming Language Compiled in Java. It's Also a Python Programming Language Family

  • @MrArcherV
    @MrArcherV ปีที่แล้ว

    Python version:
    a = [8, 3, 1, 5, ]
    b = [5, 3, 4, 6, 9, ]
    c = list(set(a) & set(b))
    print(*c)

  • @SourMnMs
    @SourMnMs 4 หลายเดือนก่อน

    You forgot to time the extra 4+ hours of debugging only to realize C is stupid

  • @illyatkachov4514
    @illyatkachov4514 ปีที่แล้ว

    Lets modify the technical task: write the same thing, but it should take less than 10ms to execute

  • @nash-p
    @nash-p ปีที่แล้ว +3

    The moment I saw vi I knew shit was about to get down

  • @water1705
    @water1705 ปีที่แล้ว

    ah yes, C. The DooM programming language

  • @unknownguywholovespizza
    @unknownguywholovespizza ปีที่แล้ว +1

    Me seeing the program in C: 👁️👄👁️

  • @absolute-narwhal
    @absolute-narwhal 4 หลายเดือนก่อน +1

    The C programmer made python

  • @serijas737
    @serijas737 ปีที่แล้ว +2

    Generally curious what are the applications of finding the intersection of two unsorted arrays to begin with

  • @bruno10505
    @bruno10505 3 หลายเดือนก่อน

    Creating in C is faster, as expected

  • @vladyslavtymofieiev4213
    @vladyslavtymofieiev4213 ปีที่แล้ว

    a.stream().filter(x->b.contains(x)).collect(Collectors.toList());
    Writing this take exactly 29 seconds.

  • @TheRedBerry27
    @TheRedBerry27 4 หลายเดือนก่อน

    the very normal timer in the corner |:

  • @Lohmatiyshmel
    @Lohmatiyshmel 4 หลายเดือนก่อน +1

    All languages are equal. Equally bad just in different cases

  • @thenistthedev
    @thenistthedev ปีที่แล้ว

    If i understand correctly while it's dirty c version can be written like this, much shorter, well it's not clean and surely not the shortest but i perfer this version
    #include
    int main(void){
    int a[4]={8,3,1,5};
    int b[5]={5,3,4,6,9};
    for(int i=0;i

    • @Virbox
      @Virbox  ปีที่แล้ว

      Yeah it does the job but it doesn't create a resulting array

    • @thenistthedev
      @thenistthedev ปีที่แล้ว

      @@Virbox welp task was finding it and didnt say it to store in any form so it counts IMO

  • @tonkofonko
    @tonkofonko ปีที่แล้ว

    Brutal difference betveen PY and C like code length

  • @abyrvalg_
    @abyrvalg_ ปีที่แล้ว

    >>> s1 = set([1, 2, 3])
    >>> s2 = set([2, 3, 4])
    >>> s1 & s2
    it is even easier using sets.

  • @sledgex9
    @sledgex9 ปีที่แล้ว

    Middle ground. Just use C++: std::sort and then std::set_intersection

  • @cyanuranus6456
    @cyanuranus6456 ปีที่แล้ว

    Cython is a Python Programming Language Compiled in C. Cython is Just a Python Programming Language Family

  • @hotman5418
    @hotman5418 3 หลายเดือนก่อน

    to try: glichless, any%, all operands

  • @jfs3234
    @jfs3234 ปีที่แล้ว +2

    In fact in Python it's just return set(a) & set(b)

  • @dtar380
    @dtar380 3 หลายเดือนก่อน

    Bro was typing 3 times faster when programming on C

  • @eljuano28
    @eljuano28 2 ปีที่แล้ว +12

    No matter what you code in, you're cooler than the regular ole people. Every language has it's purpose.(Except Java! That doesn't even count! Sheesh!)

    • @quack3891
      @quack3891 ปีที่แล้ว +7

      well technically java does have a purpose, and the purpose is JVM and boilerplate long enough to give neck pains to whoever's reading it

    • @quenting4885
      @quenting4885 ปีที่แล้ว +6

      Minecraft

    • @notjebkerman6207
      @notjebkerman6207 3 หลายเดือนก่อน

      @@quenting4885 And FIRST FTC, which also is the only "embedded" use for Java.

  • @beanbag1415
    @beanbag1415 ปีที่แล้ว +1

    Im just learning python and this makes me want to cry.

    • @mjl3631
      @mjl3631 ปีที่แล้ว +1

      I have a degree in computer science and I work with C/C++ almost daily for my job and python still makes me want to cry. I just cannot for the life of me get comfortable with that language. It’s not for everyone!

    • @beanbag1415
      @beanbag1415 ปีที่แล้ว

      @@mjl3631 this is fair. It just feels like theres so much to learn about programming i dont know how i can possibly know enough.

    • @mjl3631
      @mjl3631 ปีที่แล้ว +2

      @@beanbag1415 There is a lot to learn, and no single human will ever learn everything there is to know about programming.
      Not sure if you’re looking for advice, but I understand how frustrating is to start out. Focus on one problem at a time, and even break it down into smaller problems if you can. And don’t think for a moment that if you’re doing something wrong, or make a mistake, that you’re failing. Every single programmer, from beginners to the highest level researchers, etc, all make mistakes way more often than you would think. It’s a weird mindset to adapt to, but making mistakes is necessary.
      There’s no way around the frustration of syntax errors though unfortunately. It gets easier the more you learn and practice.

    • @beanbag1415
      @beanbag1415 ปีที่แล้ว

      @@mjl3631 thank you for this!

  • @tedjerin2966
    @tedjerin2966 ปีที่แล้ว +1

    “All languages are equal” some are just less equal than others! 😂