Python programmer vs C programmer (speedrun)

Sdílet
Vložit
  • čas přidán 20. 08. 2022
  • 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)
  • Zábava

Komentáře • 866

  • @Tekay37
    @Tekay37 Před rokem +7399

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

  • @dan00b8
    @dan00b8 Před rokem +2513

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

    • @NotTheHeroStudios
      @NotTheHeroStudios Před rokem +50

      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 Před rokem +3

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

    • @unknownguy5559
      @unknownguy5559 Před rokem +5

      @@scartyz762 he has ascended.

    • @maxisource
      @maxisource Před měsícem

      its a scary language but i love it

  • @TileBitan
    @TileBitan Před rokem +5943

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

    • @gtxg.
      @gtxg. Před rokem +584

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

    • @creepynutsrl
      @creepynutsrl Před rokem +335

      @@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 Před rokem +300

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

    • @marble17
      @marble17 Před rokem +6

      I see smiling face in your pfp

    • @eljuano28
      @eljuano28 Před rokem

      Naw, he's got gas.

  • @onee
    @onee Před rokem +3333

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

    • @zm5856
      @zm5856 Před rokem +23

      🤓

    • @Cypekeh
      @Cypekeh Před rokem +79

      probably vim

    • @MasterAdrenalyn
      @MasterAdrenalyn Před rokem +68

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

    • @abdelidrissi7241
      @abdelidrissi7241 Před rokem +26

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

    • @bforbiggy
      @bforbiggy Před rokem +65

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

  • @steveoc64
    @steveoc64 Před rokem +2565

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

    •  Před rokem +26

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

    • @harrytsang1501
      @harrytsang1501 Před rokem +59

      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 Před rokem +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 Před rokem +3

      @@Israel220500 👍

    • @prodbytukoo
      @prodbytukoo Před rokem +7

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

  • @vaulttectradingco8438
    @vaulttectradingco8438 Před rokem +382

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

    • @homerlol9058
      @homerlol9058 Před rokem +102

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

    • @nathanaeltrimm2720
      @nathanaeltrimm2720 Před rokem +20

      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 Před rokem +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 Před rokem +31

      @@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 Před rokem +11

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

  • @ademyro
    @ademyro Před rokem +664

    I like the fact that he uses vi just for C

    • @macreator9497
      @macreator9497 Před rokem +16

      Using vi is enough

    • @Pikopati
      @Pikopati Před rokem +28

      I like the fact he can close it

    • @Mmnc-bv3rk
      @Mmnc-bv3rk Před rokem +39

      Not even vim, just vi...

    • @edward8064
      @edward8064 Před rokem +7

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

    • @anstropleuton
      @anstropleuton Před rokem +11

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

  • @bayzed
    @bayzed Před rokem +778

    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 Před rokem +126

      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  Před rokem +202

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

    • @prodbytukoo
      @prodbytukoo Před rokem +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++

    • @badonker
      @badonker Před rokem +184

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

    • @Gandarf_
      @Gandarf_ Před rokem +53

      Or just use hashmap....

  • @St418_
    @St418_ Před rokem +168

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

    • @davidu9904
      @davidu9904 Před rokem +22

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

    • @Meridian-lk2fo
      @Meridian-lk2fo Před rokem +4

      Woah

    • @mousepotatoliteratureclub
      @mousepotatoliteratureclub Před rokem +3

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

    • @stackercoding2054
      @stackercoding2054 Před rokem +3

      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_ Před rokem +4

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

  • @EarlyBitcoiner
    @EarlyBitcoiner Před rokem +1214

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

    • @squareeyes7386
      @squareeyes7386 Před rokem +139

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

    • @ignacio1085
      @ignacio1085 Před rokem +23

      @@squareeyes7386 u ok, bro?

    • @quack3891
      @quack3891 Před rokem +52

      @@ignacio1085 he's having an existential crisis

    • @adil080_
      @adil080_ Před rokem +19

      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 Před rokem +48

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

  • @leu9455
    @leu9455 Před rokem +115

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

    • @dingding4898
      @dingding4898 Před rokem +6

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

    • @blitzpark1250
      @blitzpark1250 Před rokem +10

      @@dingding4898 hes god

    • @leu9455
      @leu9455 Před rokem +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 Před rokem +4

      @@dingding4898 i think it is called unpacking

    • @dingding4898
      @dingding4898 Před rokem

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

  • @Pokedollar
    @Pokedollar Před rokem +222

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

    • @radmir_khusnutdinov
      @radmir_khusnutdinov Před rokem +35

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

    • @RurikLoderr
      @RurikLoderr Před rokem +43

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

    • @Bl0xxy
      @Bl0xxy Před měsícem +2

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

    • @IamPyu-v
      @IamPyu-v Před měsícem +1

      @@Bl0xxy He's not using Windows

    • @Bl0xxy
      @Bl0xxy Před měsícem

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

  • @Eknoma
    @Eknoma Před rokem +162

    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 Před rokem

      fuck of brother, let us be stupid

    • @nkoninn
      @nkoninn Před rokem +3

      Your comment just reminded me about this video: czcams.com/video/oTEiQx88B2U/video.html 😂

  • @user-hn3zy6px3l
    @user-hn3zy6px3l Před rokem +31

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

    • @linuxization4205
      @linuxization4205 Před rokem

      The compile time of python is sprinkled into the runtime speed

    • @weatherman1504
      @weatherman1504 Před rokem

      @@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 Před měsícem +3

      It is indeed very fast at segfaulting

    • @notjebkerman6207
      @notjebkerman6207 Před 11 dny

      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().

  • @Elfcheg
    @Elfcheg Před rokem +143

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

  • @BassHero55
    @BassHero55 Před rokem +73

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

    • @georgegriffin6114
      @georgegriffin6114 Před rokem +27

      They are still trying to satisfy the borrow checker

    • @butterybread4162
      @butterybread4162 Před rokem +10

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

    • @KohuGaly
      @KohuGaly Před rokem +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 Před rokem +1

      *Rust, Dust and Guts starts playing*

    • @user-hp7dc4bv4j
      @user-hp7dc4bv4j Před 12 dny

      why do you have emojis?

  • @gelcendaa
    @gelcendaa Před rokem +11

    Really nice and helpful... Thanks!

  • @electra_
    @electra_ Před rokem +70

    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 Před rokem +49

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

  • @cookie_space
    @cookie_space Před měsícem +4

    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.

  • @DaJodad
    @DaJodad Před rokem +30

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

    • @keris3920
      @keris3920 Před rokem +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 Před rokem +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 Před rokem +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 Před rokem

      The compile time of python is sprinkled into the runtime speed

    • @keris3920
      @keris3920 Před rokem

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

  • @mrmaniac9905
    @mrmaniac9905 Před rokem +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  Před rokem +26

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

    • @mrmaniac9905
      @mrmaniac9905 Před rokem +10

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

    • @zm5856
      @zm5856 Před rokem +5

      @@mrmaniac9905 🤓

    • @buffcode
      @buffcode Před rokem

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

    • @zm5856
      @zm5856 Před rokem +1

      @@buffcode new phone who dis?

  • @thegreatken2073
    @thegreatken2073 Před rokem +67

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

    • @ninstagram
      @ninstagram Před rokem +47

      slightly more than 0 seconds for both

    • @Hardcore_Remixer
      @Hardcore_Remixer Před rokem +1

      @@ninstagram For 5000 elements in each array?

    • @user-uo1iw5ht5t
      @user-uo1iw5ht5t Před rokem +2

      @@Hardcore_Remixer still 0 seconds

    • @k1_26h
      @k1_26h Před 6 dny

      ​@@Hardcore_Remixer 0.1 sec

  • @jptmane
    @jptmane Před rokem +33

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

    • @alexnaosei763
      @alexnaosei763 Před rokem +3

      🤓

    • @AR-yd2nd
      @AR-yd2nd Před měsícem

      Isn't numpy written in C under the hood?

    • @volbla
      @volbla Před 19 dny

      ​@@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 Před 6 dny

      ​@@AR-yd2ndis numpy also use multithreading?

    • @sergeydostovalov6180
      @sergeydostovalov6180 Před 2 dny

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

  • @stomah9832
    @stomah9832 Před rokem +8

    remember to mark your return types const just in case

  • @dariuschitu3254
    @dariuschitu3254 Před rokem +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 Před rokem +14

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

    • @jhonatasbonfim4917
      @jhonatasbonfim4917 Před rokem +1

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

    • @itellyouforfree7238
      @itellyouforfree7238 Před rokem +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 Před rokem

      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 Před rokem

      it's also easier to point out bugs unlike python

  • @warrenhenning8064
    @warrenhenning8064 Před rokem +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 Před rokem

      @Artify cleaning up memory is a very slow unnecessary process

    • @GeorgeFosberry
      @GeorgeFosberry Před rokem +9

      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 Před rokem +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 Před rokem +7

      @@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 Před rokem +4

      @@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 Před rokem +1

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

  • @donno2048
    @donno2048 Před rokem +36

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

    • @protectedmethod9724
      @protectedmethod9724 Před rokem +1

      Not if multiple values are the same

    • @itellyouforfree7238
      @itellyouforfree7238 Před rokem +1

      @@protectedmethod9724 what are you talking about?

    • @abyrvalg_
      @abyrvalg_ Před rokem

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

    • @protectedmethod9724
      @protectedmethod9724 Před rokem +1

      @@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 Před rokem +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.

  • @sid851
    @sid851 Před 11 měsíci +3

    Hands down best explanation of time dilation on the internet.

  • @DMSBrian24
    @DMSBrian24 Před rokem +4

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

  • @notyourconcern916
    @notyourconcern916 Před rokem +48

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

  • @thediabolo4421
    @thediabolo4421 Před rokem +28

    After 7min already on reddit lol

  • @Yutaro-Yoshii
    @Yutaro-Yoshii Před rokem +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 Před rokem +1

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

    • @Yutaro-Yoshii
      @Yutaro-Yoshii Před rokem +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 Před rokem

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

    • @Yutaro-Yoshii
      @Yutaro-Yoshii Před rokem

      ​@@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 Před rokem

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

  • @serijas737
    @serijas737 Před rokem +2

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

  • @trixion74
    @trixion74 Před rokem +14

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

  • @arturmg2068
    @arturmg2068 Před 6 měsíci +4

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

  • @someguyusingyournetflixaccount

    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 Před rokem

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

    • @notjebkerman6207
      @notjebkerman6207 Před 11 dny

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

  • @govindsanal3092
    @govindsanal3092 Před rokem +7

    You know shits about to get real when he types v

  • @user-yd9tm9es8b
    @user-yd9tm9es8b Před rokem +2

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

  • @raidengaming
    @raidengaming Před rokem +3

    It worked! Tank you sir.

  • @morgan0
    @morgan0 Před rokem +1

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

  • @freeshavaacadooo1095
    @freeshavaacadooo1095 Před rokem +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 Před rokem +4

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

    • @freeshavaacadooo1095
      @freeshavaacadooo1095 Před rokem +4

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

    • @rutviksaptarshi745
      @rutviksaptarshi745 Před rokem +3

      @@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 Před rokem +1

      @@rutviksaptarshi745 unordered map

    • @rutviksaptarshi745
      @rutviksaptarshi745 Před rokem +1

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

  • @beavercat7925
    @beavercat7925 Před rokem +4

    Have you tried writing both codes with the same speed?

  • @gordonzar992
    @gordonzar992 Před rokem +7

    some are more equal than others

  • @sohigh7433
    @sohigh7433 Před měsícem +1

    You just nested two loops big man

  • @ryanpmcguire
    @ryanpmcguire Před rokem

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

  • @nash-p
    @nash-p Před rokem +3

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

  • @akhildeshpande6311
    @akhildeshpande6311 Před rokem

    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

  • @vadrif-draco
    @vadrif-draco Před rokem +2

    This is so good

  • @majinfoo
    @majinfoo Před rokem

    Can't wait till I get programming down, or at least understand it alot better than I do now

  • @isaacmaia5501
    @isaacmaia5501 Před rokem

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

  • @scottsmith6861
    @scottsmith6861 Před rokem +1

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

  • @mech653
    @mech653 Před rokem +7

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

  • @supercellodude
    @supercellodude Před rokem

    Even though CPython implements a bytecode compiler internally, how much slower is this example compared to C?

  • @bartomiejlandoch5238
    @bartomiejlandoch5238 Před rokem +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 Před rokem +1

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

  • @rosyidharyadi7871
    @rosyidharyadi7871 Před rokem +6

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

    • @eddieisfiction442
      @eddieisfiction442 Před rokem +2

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

  • @stoychorusinov5519
    @stoychorusinov5519 Před měsícem

    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

  • @RandomVideos-im4ue
    @RandomVideos-im4ue Před rokem

    I learned the basics of C language but i never learned that systex you used 😟

  • @arunsharma8740
    @arunsharma8740 Před rokem +1

    This video deserves 2m views instead of 2k views

  • @JulioCesar-zy4xb
    @JulioCesar-zy4xb Před rokem +1

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

  • @MeNeXzPnz
    @MeNeXzPnz Před rokem

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

  • @theloniousMac
    @theloniousMac Před rokem

    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]

  • @hockeyplayat0567
    @hockeyplayat0567 Před rokem

    Ive been taking coding classes 2 specifcally and I just dont understand any of it, do i bother trying to continue?

  • @adl_leo_0227
    @adl_leo_0227 Před rokem +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

  • @hamzerpanzer
    @hamzerpanzer Před rokem

    I am extremely new to C, can someone explain how the int arr in the intarr structure can be used as an array if it's not defined as an array?

    • @matriculadigital3926
      @matriculadigital3926 Před 10 měsíci

      the intarr struct is storing an int *const, witch is kinda like defining an empty array before using it, then in the main function the arrays are being created along with their length, then they get stored in the struct. this is how the function knows that they are going to be arrays, therefore they can be iterated, once you understand pointers you can do a lot of stuff in C I highly recommend Practical C Programming, 3rd Edition by Steve Oualline, it will help you understand pointers and how amazing C is

  • @MsNameness
    @MsNameness Před rokem

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

  • @berkaybakacak
    @berkaybakacak Před rokem +1

    this video made my day :)

  • @unknownlordd
    @unknownlordd Před rokem +10

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

  • @jfs3234
    @jfs3234 Před rokem +2

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

  • @danels7
    @danels7 Před 13 hodinami

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

  • @sangchoo1201
    @sangchoo1201 Před rokem

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

  • @zurrutikGames
    @zurrutikGames Před rokem +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

  • @lucasf.v.n.4197
    @lucasf.v.n.4197 Před rokem +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  Před rokem

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

    • @notjebkerman6207
      @notjebkerman6207 Před 11 dny

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

  • @saeedmahmoodi7211
    @saeedmahmoodi7211 Před rokem +5

    I was always having trouble explaining people why they should learn c/c++ over any other language specially when they are a beginner; thank you 🙏 now i only have to share this

    • @xCwieCHRISx
      @xCwieCHRISx Před 10 měsíci +1

      First i learned C then C++. The benefit is when u understood C++ its easy to learn any other programming language.

  • @williambaldwin9346
    @williambaldwin9346 Před rokem

    When that DOOM music kicks in...

  • @AProbablyPostman
    @AProbablyPostman Před rokem

    Needs more setters and getters.

  • @Awwe12675
    @Awwe12675 Před rokem

    Please what program did you use for this montage

  • @eljuano28
    @eljuano28 Před rokem +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 Před rokem +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 Před rokem +6

      Minecraft

    • @ZaHandle
      @ZaHandle Před rokem +3

      Java is only for Minecraft

    • @notjebkerman6207
      @notjebkerman6207 Před 11 dny

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

  • @HXTz0
    @HXTz0 Před rokem +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  Před rokem +1

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

    • @HXTz0
      @HXTz0 Před rokem +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 Před rokem +2

      that looks horrible wtf are u talking about lol

    • @HXTz0
      @HXTz0 Před rokem

      @@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 Před rokem

      @@HXTz0 simpler does not mean pretty.

  • @beanbag1415
    @beanbag1415 Před rokem +1

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

    • @mjl3631
      @mjl3631 Před rokem +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 Před rokem

      @@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 Před rokem +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 Před rokem

      @@mjl3631 thank you for this!

  • @Alpharabius99
    @Alpharabius99 Před rokem +1

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

  • @petersagitarius4356
    @petersagitarius4356 Před rokem +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.

  • @tonymercier6812
    @tonymercier6812 Před rokem

    Python's syntax hurts my brain
    * C also does, but I've never had to use it

  • @PepeTheJoker
    @PepeTheJoker Před rokem

    I'm new to programming but *HOLY SHIT*

  • @mr_don_key
    @mr_don_key Před rokem

    why is the c video part played back faster? (nobody types that fast, without errors, even with tab completion)

  • @Hvleos
    @Hvleos Před 18 dny

    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.

  • @loganreidy7055
    @loganreidy7055 Před rokem +2

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

  • @athiradha262
    @athiradha262 Před rokem

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

  • @thenasiudk1337
    @thenasiudk1337 Před 10 dny

    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

  • @szebasztianjoo8980
    @szebasztianjoo8980 Před rokem

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

  • @Chickita000
    @Chickita000 Před rokem

    Now call the function 1000000000 times and speerun execution of files 😎

  • @tonkofonko
    @tonkofonko Před rokem

    Brutal difference betveen PY and C like code length

  • @luberius
    @luberius Před rokem +3

    hahah not just speed, even the editor is different

  • @ShotgunLlama
    @ShotgunLlama Před rokem

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

  • @NTLMBigBench
    @NTLMBigBench Před rokem +3

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

    • @DaMonster
      @DaMonster Před rokem

      This c code is a c++ solution tho

    • @NTLMBigBench
      @NTLMBigBench Před rokem

      @@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 Před rokem

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

  • @wadysal
    @wadysal Před 11 dny

    It's impossible to compare witout understand the paltaform. If I need program a microcontrollero or a PC with all recourses.

  • @user-io9np5ef3h
    @user-io9np5ef3h Před rokem

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

  • @AndrewTSq
    @AndrewTSq Před rokem

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

  • @Omena0
    @Omena0 Před měsícem

    Wait cant you just use the & operator to check for intersections?