Python programmer vs C programmer (speedrun)

Поділитися
Вставка

КОМЕНТАРІ • 878

  • @Tekay37
    @Tekay37 2 роки тому +7619

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

    • @cdey3886
      @cdey3886 2 роки тому +280

      He uses Vi that's why

    • @doctorbobstone
      @doctorbobstone 2 роки тому +233

      The C sound track is pretty good, too.

    • @oxycodin2253
      @oxycodin2253 2 роки тому +32

      @@doctorbobstone Doom OST - Meathook

    • @ennead322
      @ennead322 2 роки тому +69

      Probably a gaming chair helped as well?

    • @Oscky2013
      @Oscky2013 2 роки тому +2

      @@ennead322 not really

  • @TileBitan
    @TileBitan 2 роки тому +6086

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

    • @gtxg.
      @gtxg. 2 роки тому +598

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

    • @creepynutsrl
      @creepynutsrl 2 роки тому +345

      @@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 2 роки тому +307

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

    • @marble17
      @marble17 2 роки тому +6

      I see smiling face in your pfp

    • @eljuano28
      @eljuano28 2 роки тому

      Naw, he's got gas.

  • @steveoc64
    @steveoc64 2 роки тому +2670

    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 2 роки тому +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 2 роки тому +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 2 роки тому +3

      @@Israel220500 👍

    • @prodbytukoo
      @prodbytukoo 2 роки тому +8

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

  • @dan00b8
    @dan00b8 2 роки тому +2657

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

    • @NotTheHeroStudios
      @NotTheHeroStudios 2 роки тому +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 7 місяців тому +2

      its a scary language but i love it

  • @onee
    @onee 2 роки тому +3452

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

    • @zm5856
      @zm5856 2 роки тому +23

      🤓

    • @Cypekeh
      @Cypekeh 2 роки тому +78

      probably vim

    • @MasterAdrenalyn
      @MasterAdrenalyn 2 роки тому +70

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

    • @abdelidrissi7241
      @abdelidrissi7241 2 роки тому +29

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

    • @bforbiggy
      @bforbiggy 2 роки тому +68

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

  • @vaulttectradingco8438
    @vaulttectradingco8438 2 роки тому +427

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

    • @homerlol9058
      @homerlol9058 2 роки тому +116

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

    • @nathanaeltrimm2720
      @nathanaeltrimm2720 2 роки тому +26

      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 2 роки тому +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 2 роки тому +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 2 роки тому +12

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

  • @cookie_space
    @cookie_space 6 місяців тому +27

    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.

  • @leu9455
    @leu9455 2 роки тому +122

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

    • @dingding4898
      @dingding4898 2 роки тому +7

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

    • @blitzpark1250
      @blitzpark1250 2 роки тому +10

      @@dingding4898 hes god

    • @leu9455
      @leu9455 2 роки тому +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 2 роки тому +5

      @@dingding4898 i think it is called unpacking

    • @dingding4898
      @dingding4898 2 роки тому

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

  • @St418_
    @St418_ 2 роки тому +184

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

    • @davidu9904
      @davidu9904 2 роки тому +23

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

    • @Meridian-lk2fo
      @Meridian-lk2fo 2 роки тому +5

      Woah

    • @mousepotatoliteratureclub
      @mousepotatoliteratureclub 2 роки тому +3

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

    • @stackercoding2054
      @stackercoding2054 2 роки тому +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_ 2 роки тому +6

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

  • @ademyro
    @ademyro 2 роки тому +685

    I like the fact that he uses vi just for C

    • @macreator9497
      @macreator9497 2 роки тому +16

      Using vi is enough

    • @Pikopati
      @Pikopati 2 роки тому +28

      I like the fact he can close it

    • @Mmnc-bv3rk
      @Mmnc-bv3rk 2 роки тому +41

      Not even vim, just vi...

    • @edward8064
      @edward8064 2 роки тому +7

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

    • @anstropleuton2
      @anstropleuton2 2 роки тому +11

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

  • @bayzed
    @bayzed 2 роки тому +790

    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 2 роки тому +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  2 роки тому +207

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

    • @prodbytukoo
      @prodbytukoo 2 роки тому +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 2 роки тому +189

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

    • @Gandarf_
      @Gandarf_ 2 роки тому +54

      Or just use hashmap....

  • @Pokedollar
    @Pokedollar 2 роки тому +231

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

    • @radmir_khusnutdinov
      @radmir_khusnutdinov 2 роки тому +38

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

    • @RurikLoderr
      @RurikLoderr 2 роки тому +46

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

    • @Bl0xxy
      @Bl0xxy 6 місяців тому +3

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

    • @IamPyu-v
      @IamPyu-v 6 місяців тому +1

      @@Bl0xxy He's not using Windows

    • @Bl0xxy
      @Bl0xxy 6 місяців тому

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

  • @Elfcheg
    @Elfcheg 2 роки тому +152

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

  • @МаксимКайтмазян-л1ю
    @МаксимКайтмазян-л1ю 2 роки тому +36

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

    • @linuxization4205
      @linuxization4205 2 роки тому

      The compile time of python is sprinkled into the runtime speed

    • @weatherman1504
      @weatherman1504 2 роки тому

      @@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 6 місяців тому +4

      It is indeed very fast at segfaulting

    • @notjebkerman6207
      @notjebkerman6207 6 місяців тому

      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 2 роки тому +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 2 роки тому

      fuck of brother, let us be stupid

    • @nkoninn
      @nkoninn 2 роки тому +3

      Your comment just reminded me about this video: ua-cam.com/video/oTEiQx88B2U/v-deo.html 😂

  • @BassHero55
    @BassHero55 2 роки тому +77

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

    • @georgegriffin6114
      @georgegriffin6114 2 роки тому +30

      They are still trying to satisfy the borrow checker

    • @butterybread4162
      @butterybread4162 2 роки тому +11

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

    • @KohuGaly
      @KohuGaly 2 роки тому +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 2 роки тому +1

      *Rust, Dust and Guts starts playing*

    • @김인영-q5x
      @김인영-q5x 6 місяців тому

      why do you have emojis?

  • @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 2 роки тому +140

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

    • @ignacio1085
      @ignacio1085 2 роки тому +22

      @@svlmain u ok, bro?

    • @quack3891
      @quack3891 2 роки тому +51

      @@ignacio1085 he's having an existential crisis

    • @adil080_
      @adil080_ 2 роки тому +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 2 роки тому +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

  • @mekatronikachmadi5472
    @mekatronikachmadi5472 2 роки тому +51

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

  • @DaJodad
    @DaJodad 2 роки тому +34

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

    • @keris3920
      @keris3920 2 роки тому +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 2 роки тому +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 2 роки тому +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 2 роки тому

      The compile time of python is sprinkled into the runtime speed

    • @keris3920
      @keris3920 2 роки тому

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

  • @gelcendaa
    @gelcendaa 2 роки тому +11

    Really nice and helpful... Thanks!

  • @stomah9832
    @stomah9832 2 роки тому +8

    remember to mark your return types const just in case

  • @electra_
    @electra_ 2 роки тому +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

  • @mrmaniac9905
    @mrmaniac9905 2 роки тому +83

    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  2 роки тому +26

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

    • @mrmaniac9905
      @mrmaniac9905 2 роки тому +10

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

    • @zm5856
      @zm5856 2 роки тому +5

      @@mrmaniac9905 🤓

    • @buffcode
      @buffcode 2 роки тому

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

    • @zm5856
      @zm5856 2 роки тому +1

      @@buffcode new phone who dis?

  • @notfinxx
    @notfinxx 2 роки тому +2

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

  • @thegreatken2073
    @thegreatken2073 2 роки тому +67

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

  • @arturmg2068
    @arturmg2068 Рік тому +4

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

    • @EaZea
      @EaZea 5 місяців тому +1

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

    • @arturmg2068
      @arturmg2068 5 місяців тому

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

  • @trixion74
    @trixion74 2 роки тому +15

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

  • @zhtjrxjhethgvh
    @zhtjrxjhethgvh 2 роки тому +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 2 роки тому

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

    • @notjebkerman6207
      @notjebkerman6207 6 місяців тому

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

  • @DMSBrian24
    @DMSBrian24 2 роки тому +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 4 місяці тому

      Also has better runtime 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 2 роки тому

      @Artify cleaning up memory is a very slow unnecessary process

    • @GeorgeFosberry
      @GeorgeFosberry 2 роки тому +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 2 роки тому +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 2 роки тому +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 2 роки тому +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

  • @govindsanal3092
    @govindsanal3092 2 роки тому +8

    You know shits about to get real when he types v

  • @thediabolo4421
    @thediabolo4421 2 роки тому +30

    After 7min already on reddit lol

  • @donno2048
    @donno2048 2 роки тому +39

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

    • @protectedmethod9724
      @protectedmethod9724 2 роки тому +1

      Not if multiple values are the same

    • @itellyouforfree7238
      @itellyouforfree7238 2 роки тому +1

      @@protectedmethod9724 what are you talking about?

    • @abyrvalg_
      @abyrvalg_ 2 роки тому

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

    • @protectedmethod9724
      @protectedmethod9724 2 роки тому +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 2 роки тому +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.

  • @Yutaro-Yoshii
    @Yutaro-Yoshii 2 роки тому +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 2 роки тому +1

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

    • @Yutaro-Yoshii
      @Yutaro-Yoshii 2 роки тому +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 2 роки тому

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

    • @Yutaro-Yoshii
      @Yutaro-Yoshii 2 роки тому

      ​@@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 2 роки тому

      @@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)?

  • @dariuschitu3254
    @dariuschitu3254 2 роки тому +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 2 роки тому +14

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

    • @jhonatasbonfim4917
      @jhonatasbonfim4917 2 роки тому +1

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

    • @itellyouforfree7238
      @itellyouforfree7238 2 роки тому +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 2 роки тому

      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

  • @jptmane
    @jptmane 2 роки тому +35

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

    • @alexnaosei763
      @alexnaosei763 2 роки тому +3

      🤓

    • @AR-yd2nd
      @AR-yd2nd 6 місяців тому

      Isn't numpy written in C under the hood?

    • @volbla
      @volbla 6 місяців тому

      ​@@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 5 місяців тому

      ​@@AR-yd2ndis numpy also use multithreading?

    • @gatos-su
      @gatos-su 5 місяців тому

      @@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.

  • @gordonzar992
    @gordonzar992 2 роки тому +7

    some are more equal than others

  • @raidengaming
    @raidengaming 2 роки тому +3

    It worked! Tank you sir.

  • @nash-p
    @nash-p 2 роки тому +3

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

  • @bartomiejlandoch5238
    @bartomiejlandoch5238 2 роки тому +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 2 роки тому +1

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

  • @Trumputin_povelitel_telezombi
    @Trumputin_povelitel_telezombi 2 роки тому +48

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

  • @thenasiudk1337
    @thenasiudk1337 5 місяців тому

    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

  • @serijas737
    @serijas737 2 роки тому +2

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

  • @zurrutikGames
    @zurrutikGames 2 роки тому +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 3 місяці тому

      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 };

  • @burnin8orable
    @burnin8orable 2 роки тому +1

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

    • @rc8s
      @rc8s Рік тому +1

      Or just use Lua with C

  • @freeshavaacadooo1095
    @freeshavaacadooo1095 2 роки тому +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 2 роки тому +4

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

    • @freeshavaacadooo1095
      @freeshavaacadooo1095 2 роки тому +4

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

    • @rutviksaptarshi745
      @rutviksaptarshi745 2 роки тому +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 2 роки тому +1

      @@rutviksaptarshi745 unordered map

    • @rutviksaptarshi745
      @rutviksaptarshi745 2 роки тому +1

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

  • @Кирилл-ц2ю6ш
    @Кирилл-ц2ю6ш 2 роки тому +1

    Why don't just
    def intersection(a, b):
    return list(set(a) & set(b))

  • @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

  • @jfs3234
    @jfs3234 2 роки тому +2

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

  • @beavercat7925
    @beavercat7925 2 роки тому +4

    Have you tried writing both codes with the same speed?

  • @sohigh7433
    @sohigh7433 7 місяців тому +1

    You just nested two loops big man

  • @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]

  • @mech653
    @mech653 Рік тому +7

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

  • @lucasf.v.n.4197
    @lucasf.v.n.4197 2 роки тому +2

    In ur first calloc call, u need to pass size of int right? Which is 2 or 4 bytes; I would have passed sizeof(int) instead ;why did u pass 1 then?

    • @Virbox
      @Virbox  2 роки тому

      You are correct, it is a mistake - and a demonstration why no one should "speedrun" C :)

    • @notjebkerman6207
      @notjebkerman6207 6 місяців тому

      ​@@Virbox Just drill the correct way in by practicing. That should handle your unfreed calloc as well.

  • @unknownguywholovespizza
    @unknownguywholovespizza 2 роки тому +1

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

  • @NTLMBigBench
    @NTLMBigBench 2 роки тому +3

    Now do it with the most GOAT’ed language of them all: C++

    • @DaMonster
      @DaMonster 2 роки тому

      This c code is a c++ solution tho

    • @NTLMBigBench
      @NTLMBigBench 2 роки тому

      @@DaMonster As somewhat of an expert in not knowing enough about coding to actually understand what you meant but is learning c++ and is having fun doing it, my answer is this: Ok🗿

    • @sharpedged7830
      @sharpedged7830 2 роки тому

      @@DaMonster sure, but in c++ you can write it in a shorter and prettier way, not to mention it would be easier to optimize it to O(n+m) as c++ has a builtin hashmap.

  • @JulioCesar-zy4xb
    @JulioCesar-zy4xb 2 роки тому +1

    u did not need to build a function, the collection set in python has a built in intersection method

  • @bloody1787
    @bloody1787 2 роки тому +1

    C: type* pointer = NULL; python: WHAT?! POINTERS?!??!!

  • @rosyidharyadi7871
    @rosyidharyadi7871 2 роки тому +6

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

    • @eddieisfiction442
      @eddieisfiction442 2 роки тому +2

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

  • @scottsmith6861
    @scottsmith6861 2 роки тому +1

    I mostly just appreciated the music change when it went to C lol

  • @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 2 роки тому +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

    • @x-_-_-Drayz
      @x-_-_-Drayz 2 роки тому +6

      Minecraft

    • @notjebkerman6207
      @notjebkerman6207 6 місяців тому

      @@x-_-_-Drayz And FIRST FTC, which also is the only "embedded" use for Java.

  • @tedjerin2966
    @tedjerin2966 2 роки тому +1

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

  • @χάθηκα
    @χάθηκα 2 роки тому +2

    I miss myself that don't know anything about programming. I hate thinking syntaxes alot in mind everytime curiosity hits so exhausting.

  • @loganreidy7055
    @loganreidy7055 2 роки тому +2

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

  • @JSzitas
    @JSzitas 2 роки тому

    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 2 роки тому

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

    • @JSzitas
      @JSzitas 2 роки тому

      @@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.

  • @beanbag1415
    @beanbag1415 2 роки тому +1

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

    • @mjl3631
      @mjl3631 2 роки тому +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 2 роки тому

      @@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 роки тому +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 2 роки тому

      @@mjl3631 thank you for this!

  • @morgan0
    @morgan0 2 роки тому +1

    pretty sure it could be faster in nim, convert each to a set, intersect them, and then echo that

  • @ShotgunLlama
    @ShotgunLlama 2 роки тому

    intersect = lambda a, b: set(a).intersection(b)
    If you don't need repeats

  • @akhildeshpande6311
    @akhildeshpande6311 2 роки тому

    i feel like the python code could just be return (set(a).intersection(set(b)) or whatever so its like faster than checking the second array every time we add a number
    hashsets and whatnot

  • @Hvleos
    @Hvleos 6 місяців тому

    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.

  • @emilmoroder2163
    @emilmoroder2163 2 роки тому

    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)}")

  • @HXTz0
    @HXTz0 2 роки тому +13

    Nim compiles to C(or llvm/c++/js/node) and honestly is as readable as anything.
    *```*
    *var*
    *a = @[8, 3, 1, 5]*
    *b = @[5 ,3, 4, 6, 9]*
    *func intersects(a, b: seq): seq =*
    *for i in a:*
    *if i in b: result.add i*
    *# Equivalent calls*
    *echo a.intersects b*
    *echo intersects(a, b)*
    *```*

    • @Virbox
      @Virbox  2 роки тому +1

      Thanks for the suggestion, Nim seems like a really nice language. But how it compares to Zig (besides syntax)?

    • @HXTz0
      @HXTz0 2 роки тому +4

      To me Zig feels like someone got really fed up writing C in a team. And is more barebones but with very pure C integration. They put "Simple language, no macros" on their website as a feature.
      Nim feels like someone got really fed up with crappy abstractions and decided to make a language so expressive; That in team settings you better encourage everyone to use the least expressive features that suit the job so you do not end up in hell. Macros are wild, for good, and bad.

    • @okie9025
      @okie9025 2 роки тому +2

      that looks horrible wtf are u talking about lol

    • @HXTz0
      @HXTz0 2 роки тому

      @@okie9025 Oh, what makes you think so?
      I'm not sure anything simpler is possible without losing information tbf😅
      But I get that some of it is taste.

    • @vornamenachname594
      @vornamenachname594 2 роки тому

      @@HXTz0 simpler does not mean pretty.

  • @petersagitarius4356
    @petersagitarius4356 Рік тому +1

    Maybe it could be a good idea to compare a running time also for larger arrays (with approx. 10000 numbers). In my experience, python is perfect for his ability to fast development; But the C language is perfect for fast execution (usually it is running time of C approx. 8 times shorter than python ) with solid development time. Lets imagine, that you need to covert video (some movie) and this conversion takes 10 hours by C code.. Are you really able to wait 80-100 hours to conversion of video with python code ? It is extremely wasting of time and electric energy.

  • @SilverSuperGamer
    @SilverSuperGamer 6 місяців тому +1

    You should try it in assembly x86

  • @szebasztianjoo8980
    @szebasztianjoo8980 2 роки тому

    No need for List Comprehensions, there is a more python oriented way:
    def intersect(a,b):
    return a.intersection(b)

  • @Alpharabius99
    @Alpharabius99 2 роки тому +1

    Using pointer arithmetics is far more faster both in typing and running in c

  • @StarsOfMinecrafttr
    @StarsOfMinecrafttr 6 місяців тому

    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.

  • @devwing5681
    @devwing5681 Рік тому +1

    Wow I never heard that you type 2x faster in C

  • @berkaybakacak
    @berkaybakacak 2 роки тому +1

    this video made my day :)

  • @MeNeXzPnz
    @MeNeXzPnz Рік тому

    The length of the array containing the intersection can't be bigger than the smallest array.

  • @danels7
    @danels7 5 місяців тому

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

  • @needywallaby2030
    @needywallaby2030 6 місяців тому

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

  • @Lohmatiyshmel
    @Lohmatiyshmel 6 місяців тому +1

    All languages are equal. Equally bad just in different cases

  • @Fluxdeken
    @Fluxdeken Рік тому

    kids: using python
    doom guy: typical C enjoyer

  • @cmknoll3
    @cmknoll3 6 місяців тому

    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.

  • @robertobokarev439
    @robertobokarev439 2 роки тому +2

    Assembly low-level developers:
    pop myself

    • @quack3891
      @quack3891 2 роки тому +1

      it's still not that bad in assembly, since it's just comparing two arrays, you just need to store a null-terminator at the end of the two arrays stored in .data, then using pointe- oh wait, now i see the issue

  • @mbayamarcel
    @mbayamarcel 7 місяців тому +2

    And me 《 print("3 5") 》 😎😎😎😎😎

  • @sangchoo1201
    @sangchoo1201 2 роки тому

    def intersect(a, b):
    return set(a) & set(b)

  • @cyanuranus6456
    @cyanuranus6456 Рік тому

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

  • @isaacmaia5501
    @isaacmaia5501 2 роки тому

    I just wanna know where is the soundtrack at the beginning from. The one that sounds like its from a horror movie or game

  • @thenistthedev
    @thenistthedev 2 роки тому

    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  2 роки тому

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

    • @thenistthedev
      @thenistthedev 2 роки тому

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

  • @ryanpmcguire
    @ryanpmcguire 2 роки тому

    Very nice... now lets see their time to result

  • @absolute-narwhal
    @absolute-narwhal 6 місяців тому +1

    The C programmer made python

  • @stoychorusinov5519
    @stoychorusinov5519 6 місяців тому

    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

  • @arunsharma8740
    @arunsharma8740 2 роки тому +1

    This video deserves 2m views instead of 2k views

  • @EzOddz
    @EzOddz 3 місяці тому

    Instructions unclear, accidently created an operating system

  • @schedarr
    @schedarr 2 роки тому

    He must have used keyboard with RGB lights for C, there's no other explanation

  • @Alefast51
    @Alefast51 5 місяців тому

    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!

  • @MsNameness
    @MsNameness 2 роки тому

    Python solution looks like O(N^2),
    O(N) solution:
    def intersection(a, b):
    return list(set(a) & set(b))

  • @wtfdoiputhere
    @wtfdoiputhere 2 роки тому +10

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