Haskell Is Faster Than C | Prime Reacts

ThePrimeTime
7 Oct 202331:46

Summary

TLDRこのビデオスクリプトでは、Haskell言語がC言語よりも高速になる可能性について議論されています。Eric Normandは、Haskellの型システムと遅延評価がコンパイラの最適化に役立つと指摘し、C言語では難しいかもしれないデータ構造の選択とアルゴリズムの活用がHaskellで容易であることを強調します。しかしながら、C言語でも同等の最適化が可能であると主張し、実際にはプログラミングのスキルやデータ構造の選択が速度に大きな影響を与えると述べています。

Takeaways

  • 😀 HaskellはCと比較して、特定のベンチマークで高速であることが示されているが、その理由は複雑である。
  • 🤔 コードの書き方や言語の機能が、言語間の速度比較において重要な要素となる。
  • 🛠️ Haskellのコンパイラは型システムを活用して最適化を実行し、C言語よりも効率的なコード生成が可能である。
  • 📚 Haskellは遅延評価を採用しており、実行時に必要な時にのみ計算を行うことで、余計な計算を省略できる。
  • 🔄 高レベル言語はより複雑なデータ構造を簡単に扱うことができ、これにより特定の問題に対するより効率的なアルゴリズムを実装できる。
  • 💡 Haskellの純粋関数特性は、コンパイラにコードをより自由に最適化する余地を提供する。
  • 🔄 C言語は低レベルであり、データ構造やアルゴリズムの実装がHaskellほど直感的ではない。
  • 🤷‍♂️ 言語間の比較は「書きやすさ」や「開発体験」を重視するべきであり、絶対的な速度比較は適切でない。
  • 👨‍🏫 Haskellは純粋関数プログラミングを促進し、メモリ管理やガベージコレクションなどの問題を簡素化する。
  • 🏗️ C言語は高度なデータ構造を手動で実装する必要があり、これにはより高度なスキルが必要である。
  • 🤓 高レベル言語は初期の開発では高速であるが、複雑な問題ではパフォーマンスの問題が発生する可能性がある。

Q & A

  • HaskellがCより高速であるとされるのはなぜですか?

    -Haskellは高度な型システムと純粋関数型プログラミングの特性を持っており、コンパイラが多くの最適化を行えるためです。特に遅延評価により、実行されないコードを省略することができます。

  • なぜC言語のプログラムが遅くなることがあるのですか?

    -多くの場合、プログラマがC言語のコードを最適に書けないことが原因です。C言語は非常に低レベルな操作を要求し、メモリ管理や並列処理が難しいため、効率的なコードを書くのが難しいです。

  • Haskellの型システムがプログラムの性能にどのように影響しますか?

    -Haskellの型システムはアルジェブラ型など高度な型を使用し、コンパイラに多くの情報を提供します。これにより、プログラムの安全性と最適化の可能性が高まります。

  • C言語と比べて、Haskellの利点は何ですか?

    -Haskellは純粋関数型であり、遅延評価や高度な型システムにより、メモリ管理や最適化が自動で行われやすい点が利点です。これにより、複雑なデータ構造やアルゴリズムの実装が簡単になります。

  • HaskellがCよりも最適化に優れている理由は何ですか?

    -Haskellのコンパイラはプログラムの構造に関する多くの情報を持っており、遅延評価や純粋関数の性質を利用して、不要な計算を省略するなどの最適化が可能です。

  • JavaがCよりも高速な場合があるのはなぜですか?

    -Javaはスレッドを簡単に扱えるため、並列処理がしやすく、特定のタスクにおいてCよりも高速になる場合があります。C言語ではスレッドの管理が複雑であるため、同様の最適化が難しいことがあります。

  • C言語でのリンクリストの実装が一般的なのはなぜですか?

    -C言語は低レベルであり、簡単に実装できるデータ構造としてリンクリストがよく使われます。リンクリストはシンプルで、メモリ管理が手動で行いやすいからです。

  • Haskellが提供するデータ構造の利点は何ですか?

    -Haskellは高度なデータ構造を簡単に扱うためのツールを提供しており、複雑なデータ構造やアルゴリズムを安全かつ効率的に実装することができます。

  • Haskellがコンパイル時に行う最適化の例は何ですか?

    -Haskellは純粋関数型の性質を利用してコードをインライン化したり、計算を遅延させたりすることで、不要な計算を省略する最適化を行います。

  • C言語で複雑なデータ構造を実装する際の課題は何ですか?

    -C言語ではメモリ管理やデータ構造の操作が手動で行われるため、バグが発生しやすく、コードの複雑性が増します。また、汎用性のあるデータ構造のライブラリが少なく、再利用が難しいです。

Outlines

00:00

🤔 HaskellがCより高速?

エリック・ノーマンドは、HaskellがC言語と比較して高速である主張について懐疑的な立場をとります。C言語は多くのベンチマークにおいて標準的な比較対象とされていますが、Haskellが高速であると主張される理由について、エリックは2つの利点を紹介します。1つは、Haskellのコンパイラが型システムを活用して最適化を行うことができる点で、C言語では手動での最適化が難しく、Haskellではコンパイラがより多くの「wiggle room」を持っています。2つ目は、Haskellの遅延評価(lazy evaluation)機能で、必要になるまで評価を遅延させることができるため、C言語よりも効率的なコードを書くことができるとされています。しかし、エリックはこれらがC言語に比べてHaskellが絶対的に高速であることを意味するものではないと指摘しています。

05:00

🛠 Haskellのコンパイルと最適化

エリックはHaskellのコンパイルプロセスと最適化について考察しています。HaskellはLLVMを使用してコンパイルされるため、コンパイラはプログラムの型に関する情報を活用して最適化を実行します。Haskellの遅延評価機能により、必要でない場合は評価を遅延させることができます。これにより、C言語のように厳格な評価(strict evaluation)を行う必要がなく、余計な計算を回避できます。エリックは、これらの機能がHaskellのコード生成がC言語よりも優れている理由であると述べていますが、C言語でもこれらの最適化は理論的には可能であると主張しています。

10:00

🌳 Haskellのデータ構造とアルゴリズム

エリックはHaskellが提供するデータ構造とアルゴリズムの利点を強調しています。Haskellは高レベルのデータ構造を容易に扱うことができるため、より効率的なアルゴリズムを実装することができます。例えば、リンクリストを木構造に置き換えることで、ランダムアクセスを線形時間から対数時間に改善できます。エリックは、C言語ではこれらのデータ構造を手動で実装する必要があり、Haskellではそれらが標準で提供されるため、開発者がより効率的なコードを書くことが容易であると述べています。

15:03

🔄 C言語とHaskellのデータ構造の比較

エリックはC言語とHaskellのデータ構造を比較し、C言語では基本的なデータ構造に頼る傾向があることを指摘しています。一方、Haskellではより複雑なデータ構造を標準で扱うことができるため、より効率的なアルゴリズムを実装することができます。エリックは、C言語で同等のデータ構造を実装するにはより多くの労力が必要なのに対して、Haskellではそれらが容易に利用可能であると語っています。

20:05

🤓 C言語のデータ構造の柔軟性

エリックはC言語のデータ構造の柔軟性について議論しており、C言語では基本的なデータ構造であるリンクリストから木構造までを実装することができます。しかし、Haskellではこれらのデータ構造をより簡単に扱うことができると同時に、C言語でもそれらを実装し、Haskellと比較することができます。エリックは、C言語の開発者がこれらのデータ構造をより効率的に使用することができる場合があると述べています。

25:07

📈 Haskellの開発体験とC言語の比較

エリックはHaskellの開発体験がC言語よりも優れていると述べています。Haskellは純粋関数(pure functions)を活用し、コンパイラの最適化を容易にします。これにより、Haskellの開発者はより高速なコードを書くことが容易であり、C言語のように手動での最適化を行わなくても済むとされています。エリックは、Haskellの開発体験がC言語よりも優れていると主張していますが、C言語でも同等の最適化が理論的には可能であると同時に指摘しています。

30:07

🏆 HaskellとC言語の速度比較の誤解

エリックはHaskellとC言語の速度比較に関する一般的な誤解に触れています。一般的に、X言語がC言語よりも高速であるとされる場合、実際にはC言語のコードが最適化されていないことを意味しているとエリックは指摘しています。また、HaskellがC言語よりも高速であると主張する場合、Haskellの開発者がより良い開発体験を提供していることを意味する可能性があると述べています。エリックは、HaskellやC言語のどちらが絶対的に高速であるかという議論よりも、どちらの言語を使用してより高速なコードを書くことが容易かを考えるべきだとしています。

🚀 Haskellの学習曲線と開発体験

エリックはHaskellの学習曲線と開発体験について話しており、Haskellは初心者にとっては直感的でない言語であると指摘しています。Haskellの概念を理解するには時間がかかり、その学習プロセスは困難であるとエリックは述べています。しかし、Haskellの開発体験はC言語よりも優れていると同時に強調しており、Haskellの純粋関数や遅延評価機能が開発者にとっては強力なツールであると語っています。

📝 HaskellとC言語の比較の結論

エリックはHaskellとC言語の比較の結論として、HaskellはC言語よりも高速であるという主張が誤解を招く可能性があると警告しています。HaskellはC言語に比べて優れた開発体験を提供する一方で、C言語でも同等の最適化が理論的には可能であると指摘しています。エリックは、言語間の比較は単純な速度比較ではなく、より速くコードを書くことができる言語を選ぶべきであり、その言語がHaskellであるかどうかは問わないと結び付けています。

Mindmap

Keywords

💡モナド

モナドは、プログラミング言語理論における数学的概念であり、関数型プログラミングにおけるデータ構造を表します。このビデオでは、モナドを温めるという表現が使われ、Haskell言語の機能を最大限に活用する準備をしていることを示しています。

💡Haskell

Haskellは、静的な型付けと純粋関数型プログラミングを特徴とするプログラミング言語です。ビデオではHaskellがC言語と比較して、特定のベンチマークで高速であることについて議論されています。

💡C言語

C言語は、システムプログラミングに広く使用される低水準のプログラミング言語です。ビデオでは、HaskellとC言語の性能比較が行われ、C言語の書き込みの難しさとHaskellの優れた開発体験について触れられています。

💡ベンチマーク

ベンチマークは、ソフトウェアやハードウェアの性能を測定するための標準化されたテストです。ビデオでは、HaskellとC言語のベンチマーク比較が行われ、HaskellがC言語よりも高速である場合があることを示しています。

💡関数型プログラミング

関数型プログラミングは、関数としてデータを扱うプログラミングパラダイムです。ビデオでは、Haskellが関数型プログラミング言語であることと、それがどのようにHaskellのパフォーマンスに影響を与えるかが議論されています。

💡最適化

最適化は、プログラムの実行速度やリソース使用率を高めるために行われるプロセスです。ビデオでは、Haskellのコンパイラが型システムを利用してプログラムを最適化し、C言語と比較して高速に実行される可能性があることが説明されています。

💡純粋関数

純粋関数は、同じ入力に対して常に同じ結果を返し、副作用を持ちません。ビデオでは、純粋関数がHaskell言語の最適化にどのように役立つかが議論されています。

💡データ構造

データ構造は、コンピュータサイエンスで、データの集まりを効果的に管理するための方法です。ビデオでは、Haskellが提供するデータ構造がC言語に比べて、特定の問題に対するより効率的な解決策を提供できることが強調されています。

💡メモリ管理

メモリ管理は、コンピュータプログラムが使用するメモリを効果的に割り当てると解放するプロセスです。ビデオでは、Haskellのガベージコレクタがメモリ管理を自動化し、開発者の負担を軽減する役割が説明されています。

💡並列処理

並列処理は、複数の処理を同時に実行することで、プログラムのパフォーマンスを向上させる技術です。ビデオでは、Javaが並列処理を簡単に実装できる点がC言語と比較され、それがJavaが特定のベンチマークで勝つ要因であることが議論されています。

Highlights

Haskell can be faster than C in some benchmarks due to its advanced features.

The claim that a language is faster than C often reflects the skill of the programmer rather than the language itself.

Garbage collection can be a significant advantage in higher-level languages, reducing the need to manage memory manually.

Haskell's expressive type system and pure functions allow the compiler to make optimizations that may not be possible in C.

Non-strict evaluation in Haskell can defer computations until they are actually needed, potentially improving performance.

Haskell's compiler has more 'wiggle room' for optimizations due to the language's strong typing and purity.

The performance of JavaScript can be misleading when compared to C, as it often relies on JIT compilation for small functions.

In JavaScript, the overhead of handling objects, awaits, and garbage collection can lead to significant slowdowns.

The performance difference between single-core and multi-core machines can be pronounced in JavaScript due to garbage collection optimizations.

Haskell's ability to use more complex data structures and algorithms can contribute to its speed advantage over C.

The ease of writing parallel code in higher-level languages like Java can give them an advantage in certain benchmarks.

C's low-level nature requires more manual management of data structures and threading, which can be error-prone.

Higher-level languages often provide better abstractions for data structures, making it easier to write efficient code.

The argument that Haskell is faster than C may be flawed without considering the complexity of the tasks being performed.

The ease of writing fast code is an important consideration when choosing a programming language for performance-critical applications.

Language features like generics and memory management can significantly impact the development experience and code quality.

The comparison between Haskell and C should focus on the ease of writing efficient code rather than raw speed.

Transcripts

play00:00

everyone please warm up your monads it's

play00:02

time uh hasc is very competitive with c

play00:05

and on some benchmarks it is faster how

play00:07

is that possible with all that hcll does

play00:10

on top of the raw C code how can it

play00:12

possibly be faster in this episode I

play00:15

talk about two advantages of hasal that

play00:16

can make it faster than C so typically

play00:19

this is what I say when it comes to this

play00:21

type of stuff which is when someone says

play00:24

X is faster than C what they're actually

play00:28

saying is that I didn't write C that

play00:30

good but I'm really good at writing this

play00:33

one piece of code and so therefore C is

play00:36

slower than Hull or X is faster than C

play00:40

because I can't write good C that's what

play00:42

it means okay it's like literally it is

play00:45

it's literally the original skill issue

play00:47

but I'm willing to hear it out I'm just

play00:49

saying this is skill issue number one

play00:52

this is your classic skill issue how is

play00:56

hasell faster than C my name is Eric

play00:59

Normand and I help people thrive with

play01:01

functional programming so usually

play01:04

languages are compared to C right it's

play01:06

kind of like the standard Benchmark

play01:09

language to say like oh this language is

play01:11

only a factor of two slower than C right

play01:14

most languages are slower than C by the

play01:15

way every time you see someone do a

play01:17

comparison of languages in C you know

play01:19

what they do almost 100% of the time

play01:21

they always do the worst version of it

play01:23

right like you'll see like JavaScript is

play01:24

only 1.5 times slower than C and it's

play01:27

always doing a bunch of math equations

play01:29

and you're just like dude the reality is

play01:31

that your web server everything you do

play01:34

is in a bunch of math equations is it

play01:35

surprising that JavaScript can compete

play01:37

on a small function with c yeah it's not

play01:40

that surprising because it eventually

play01:42

gets jit it gets turned into just like

play01:44

as as best representation as the engine

play01:47

can make to run this one small function

play01:50

over and over again is there anything

play01:52

impressive about that no it's not right

play01:55

because you're because that's not what

play01:56

happens what really happens is that you

play01:58

have a piece you have an object that you

play02:00

build you go and do an await you go and

play02:02

get more information you add stuff to

play02:04

the object you go and do another await

play02:06

you come back you push some stuff into

play02:08

arrays then you take that crazy object

play02:10

and arrays and you turn it into a string

play02:12

meanwhile every single response that

play02:14

comes back not only do you parse its

play02:16

Json you also send it to Zod to be

play02:19

validated in which Zod is not fast it

play02:22

then goes through and compares the

play02:23

structure and Zod to ensure that it

play02:25

actually is a correct object and

play02:27

validates at runtime that its type is

play02:29

the type expected to be and so of course

play02:32

that's where all the slowness comes from

play02:33

it does not come from a couple math

play02:37

equations it comes from all the other

play02:39

stuff because now that you've held on to

play02:40

an object over two separate uh awaits

play02:44

you've held on to the object long enough

play02:45

that maybe it got promoted from the

play02:47

nursery cach to the intergenerational

play02:49

cash and maybe it goes from the

play02:50

intergenerational one to the long-term

play02:52

one you don't even know how long you

play02:53

held on to that object and the next

play02:55

thing you know now you have major gc's

play02:56

all over the place causing large

play02:58

slowdowns maybe you're running on a

play02:59

multi-core machine but maybe you're not

play03:01

running on a multi-core machine the

play03:03

difference in performance on a non-

play03:04

multicore machine versus a multi-core

play03:06

machine is huge in JavaScript because

play03:09

all the garbage collection optimizations

play03:11

rely on multiple threads and when you

play03:13

don't have that it just gets completely

play03:16

destroyed in performance and so whenever

play03:18

I hear these things that's what I think

play03:19

of right is here is an unrealistic case

play03:22

in which we can perform nearest the same

play03:25

speed and um when especially when you

play03:28

get into like the more higher level

play03:29

languages you have this idea of like

play03:31

well we lost a lot of speed but you

play03:34

don't have to deal with all these

play03:36

problems that c has like um you know you

play03:39

don't have memory leaks because we have

play03:40

a garbage collector you don't have to

play03:41

deal with like when to when to free the

play03:43

memory garbage collector Supremacy is a

play03:45

real thing uh the fact that you don't

play03:47

like there's there seems to be nothing

play03:49

more exciting than garbage collector

play03:51

plus Arenas right you get to control a

play03:53

small amount of memory in a very fixed

play03:56

way or it's just garbage collection like

play04:00

to me that is like practically the

play04:02

perfect level of of memory management

play04:05

because for the most part you don't care

play04:06

about most memory but there are some

play04:09

memories in which you want to care about

play04:11

and to me that is like that's that's

play04:14

that's a real thing you want to because

play04:15

then you can make the parts that you

play04:16

need fast fast and the parts you don't

play04:18

care about you don't solve by cloning

play04:20

which ain't fast and you know just all

play04:23

these niceties add up but it's at the

play04:26

cost of speed but then you have

play04:27

something like haskal that

play04:30

is often faster than C in hat mentioned

play04:33

and it's faster than C let's go it's the

play04:36

ultimate

play04:37

language the benchmarks it's very close

play04:39

to see when it's not faster and it's not

play04:41

like oh it's twice as slow no it's like

play04:43

right there it's like within a few

play04:45

percentage of c and very often it's on

play04:47

the other side it's faster than C I'm

play04:49

doubting so hard right now because the

play04:51

only way I can possibly think hasell is

play04:53

faster than C is if somehow they figured

play04:55

out how to do things in less machine

play04:58

instructions right that their

play05:00

compilation is actually just genuinely

play05:02

better comp I I first off I don't even

play05:04

know if is hasell a compiled or is it is

play05:08

is there a runtime I thought there was a

play05:10

runtime which means it's not even

play05:11

compiled which makes it even more conf

play05:13

confusing but if it is compiled okay so

play05:15

it's compiled therefore it uses llvm so

play05:18

is it getting like somehow

play05:20

better better it's either or well damn

play05:23

that's

play05:24

confusing so what's going on okay let's

play05:27

see it okay so there's actually two

play05:28

things at least these are the two things

play05:30

I know of that are going on uh the first

play05:33

is optimization

play05:35

hasal has a lot of knowledge so the Haso

play05:38

compiler is given a lot of knowledge

play05:40

about the program in the form of types

play05:43

usually and those types

play05:46

are algebraic types can with an

play05:50

expressive type system prevent all sorts

play05:52

of issues at peer functional programming

play05:54

this is the function about to happen um

play05:57

than what the types in C are

play06:00

not only is it the types but it's also

play06:02

there's a lot of the semantics of has

play06:04

about to make a argument here comes give

play06:05

the compiler a lot of leeway let's call

play06:07

it a lot of wiggle room for optimization

play06:09

one of those is Again by the way all of

play06:12

the optimizations he's about to mention

play06:15

should be doable in

play06:17

C okay I'm just going to throw this out

play06:19

there I'm just going to throw it out

play06:21

there that anything that you do should

play06:23

be doable also and see I I have a very I

play06:27

I'm having a I'm having a big doubt

play06:29

coming on right now evaluation so in C

play06:32

it's strict evaluation you put a

play06:34

function there um you know it kind of

play06:36

executes one line at a time you know is

play06:38

how you should think of C executing so

play06:40

it's going to call this line everything

play06:41

on this line is going to finish before

play06:42

we get to the next line right yeah but

play06:44

in high school that's not the case what

play06:46

you can have you can call something give

play06:47

it a name and um it doesn't actually do

play06:50

anything yet right and so the compiler

play06:52

can actually analyze it and a lot of

play06:53

times figure out hey you never use this

play06:55

in this Branch right so I'm not going to

play06:57

do it yet until I know that you're in

play07:00

this branch and then I'll I'll do it or

play07:01

it might it might never do it like the

play07:03

analysis gets so complex it might just

play07:06

remember how to do it and in case you

play07:07

need it it'll be there but it won't do

play07:09

it so there's a lot of stuff that uh you

play07:11

could in theory hand tune if you were a

play07:14

a very

play07:16

uh very good at optimizing as a

play07:19

programmer you could hand tune and say

play07:21

oh there's a certain case where I won't

play07:23

need this value so I'm just not going to

play07:24

calculate until I really really need it

play07:26

but it in in practice that gets so hard

play07:29

it gets so

play07:30

complicated is that a skill issue did

play07:33

did he literally just claim skill

play07:36

issue I mean I'm not going to say like

play07:38

again garbage collectors often can lead

play07:40

you to more like better code than you

play07:43

doing it in Rust and it's a skill issue

play07:45

it's fully a skill issue the thing is in

play07:47

in Rust you'll like I've SE I've made

play07:50

rust programs slower than go programs

play07:52

because I'm not great at rust like I I

play07:54

wasn't the best person ever on it

play07:56

because I was trying to I was still in

play07:57

the learning rust phase right and so I

play07:59

was not able to write a server as fast

play08:01

as I could write it and go just simply

play08:03

because some level of garbage collection

play08:06

prevents all cloning whereas you find

play08:08

yourself making clones and doing things

play08:10

in a different way in Rust because

play08:12

you're not good at it so there is like

play08:14

most certainly I do not doubt cloning or

play08:18

skill issues play a big role in how fast

play08:19

you write your

play08:21

code that you're just not going to do it

play08:24

right and so hll can do that Haskell

play08:26

just does it in the program doesn't have

play08:28

to think about it right so you can do

play08:29

some analysis you can figure out when

play08:31

something should be is going to be

play08:33

needed anyway might as well just

play08:34

calculate it now you can do some

play08:35

analysis and say oh it's only needed

play08:36

sometimes so I'm probably won't do it

play08:38

yet and uh and then sometimes it just

play08:41

punts and just says well I don't know

play08:42

how to analyze this but the net effect

play08:45

is that that is faster another thing is

play08:48

um it can do a lot of because because

play08:50

everything is uh pure it can do a lot

play08:52

more uh optimizations of like moving

play08:55

code around and inlining and um doing

play08:59

more stuff at compile time that can't be

play09:01

done in C um and those is that statement

play09:06

true it's all llvm but is that I

play09:09

mean I could understand that if you know

play09:12

everything as peer there's some things

play09:14

you could do that would be

play09:15

different right like I I do get that

play09:18

that that that there has to be some

play09:19

level of optimization that you can do

play09:21

there but also if everything is PE it

play09:23

also means you're creating a bunch of

play09:25

new

play09:26

objects you have to Forever create new

play09:29

things that unless if it's I know I'd

play09:32

love to see some examples uh unless

play09:35

if like that's so that's like where I'm

play09:37

struggling with with is there has if

play09:41

you're making something like that there

play09:42

has to

play09:43

be coping or some other thing going on

play09:46

which is just notoriously slower than

play09:49

you know mutating memory could be the

play09:52

optim way okay

play09:55

maybe maybe show us yeah I want to see

play09:58

the video let's see let's hear here

play09:59

let's see if he does it in the video are

play10:00

typically thought of as like inlining

play10:01

people optimization that kind of stuff

play10:03

but uh Hull can has a broader range of

play10:07

Maneuvers that it can do that let the

play10:10

code get optimized really well and

play10:12

that's nice uh so what I what what I

play10:15

hear from him what I'm hearing here is

play10:16

that if you are an average hcll

play10:19

developer or an average C developer it's

play10:22

easier to write better code in hll than

play10:26

it is to write it in C which I wouldn't

play10:28

NE necessarily argue that okay that's a

play10:32

fine argument to make that you can say

play10:35

that it's easier to write like again

play10:37

when I first started learning rust it

play10:39

was easier to write a go program faster

play10:42

than it was to write a rust program

play10:43

because I kept finding myself cloning

play10:44

and doing silly things so fair totally

play10:49

fair but you can't say that in an

play10:51

absolute sense meaning that anybody

play10:54

writing C can or writing hll will ne ne

play10:59

necessarily be faster than writing C so

play11:02

it's kind of like you're getting the

play11:04

benefits of the high level like you can

play11:05

code how how how it should be read right

play11:08

I'm coding for another programmer I'm

play11:09

just making it very readable but then

play11:10

the compiler can kind of transform it

play11:12

into something that's better to be

play11:13

executed on the machine okay that's

play11:16

number one that's optimization but

play11:18

number two is potentially even bigger

play11:20

and that is that oh gosh hasal lets you

play11:23

use better data and algorithms and I

play11:26

need to explain because okay yes they're

play11:28

both through in complete okay so let's

play11:29

take that argument I agree but let's

play11:31

take it off the table no one made that

play11:33

argument ran kril though that does sound

play11:35

like an argument uh hkers would make

play11:37

well are they okay they are touring

play11:38

complete okay just making sure uh

play11:41

actually made this argument and that's

play11:42

where I got the idea for this episode

play11:44

from he was talking about rust but it

play11:45

applies equally to hll so he has been

play11:48

doing a lot of system programming in his

play11:49

career and a lot of it is done in C

play11:51

because it needs to be lowlevel and his

play11:54

his argument goes like this well see you

play11:56

know if you need a data structure um

play11:59

it's and see it's just kind of hard to

play12:00

do anything more complex than a link

play12:02

list or you know maybe you could get a

play12:03

little bit more complicated but link

play12:05

list is kind of you know you write link

play12:08

list all day long because that's like

play12:09

it's something that you know you're not

play12:10

going to mess up and it oh man you just

play12:13

got question marks from low-level

play12:14

learning when you get this many question

play12:16

marks from llll you know he's upset when

play12:19

they when he says this this isn't a good

play12:21

representation right you're kind of

play12:22

straw Manning the average C developer or

play12:24

what the average C development

play12:26

experience is oh since you can only do

play12:28

link list and C that's what you do I

play12:30

mean granted I've written a lot of Link

play12:32

list and C so fair take in that sense

play12:35

that you do write link list and see

play12:37

they're very great to write it but you

play12:38

can also write a tree and see a linked

play12:40

list is a tree it's just a one-sided

play12:43

weighted tree right it's the worst

play12:45

optimization of a tree you can also

play12:48

write arrays you can also write Dynamic

play12:50

arrays in C does the job and it's fine

play12:52

right hash well can actually because

play12:54

it's higher level it can actually manage

play12:56

much more complex data structures and do

play12:59

so in a correct way right so you can

play13:01

write it gives you tools to write data

play13:03

structures that are known to be more

play13:06

efficient for certain access patterns

play13:08

right and linked lists are very linear

play13:11

they so so every you know not every

play13:13

access if you access the first thing

play13:15

it's not it's constant but in general

play13:17

you're accessing things Conant inside

play13:20

the list uh randomly right let's just

play13:22

say that's what your algorithm is and so

play13:25

any access so you're so you're accessing

play13:27

stuff randomly that's linear and so if

play13:28

you do that in Loop wow you're quadratic

play13:29

already right

play13:31

so in hll you can replace that link list

play13:36

with something else like let's say a

play13:39

tree right so now you're act I just

play13:41

claimed I just made the claim that a

play13:43

linked list is a tree let me explain why

play13:46

I say a linked list is a tree all right

play13:48

so we got a little linked list here

play13:50

right a goes to b b goes to C okay are

play13:53

you ready for this are you

play13:57

ready it's it's a

play13:59

tree yeah yeah the conspiracy theories

play14:04

were correct linked lists are an effing

play14:07

tree yeah this is an unbalanced binary

play14:10

search tree on accident didn't even I

play14:13

know I know people people are shook

play14:16

right now just I'm just saying that we

play14:19

could totally do this hold on on the

play14:22

only Point here is that most data

play14:23

structures are somewhat swappable like

play14:25

your implementation can be abstracted

play14:27

over data structures that is still

play14:29

copium it is still copium I agree with

play14:31

it but I you know I'm just letting you

play14:34

know like when you say something like

play14:36

you can replace a linked list with a

play14:37

tree you've effectively said nothing at

play14:40

all right because a linked list is a

play14:42

tree now if you wish to have a second

play14:45

link instead of having say a next you

play14:48

can just name it left instead of next

play14:51

and then you can have right for the

play14:53

other side so you're saying a link list

play14:55

is just a unary tree I mean really it's

play14:57

just a it's just a poor L formed binary

play15:00

tree if you think about it that's all it

play15:02

is is it's just a bad binary tree now

play15:05

you could say okay well technically you

play15:07

don't use the memory position for each

play15:09

one which is also a graph it's also a

play15:12

graph this one would be an ayylic

play15:15

directed graph commonly referred to as a

play15:17

dag okay um I have a meeting at 1300 but

play15:20

I want to see how this ends okay I'll

play15:22

keep on going I'm gonna keep on going

play15:23

I'm sorry I'm just I just need to get

play15:25

that out there just because it's it's

play15:26

very important for people to understand

play15:28

that that point access is logarithmic

play15:30

you've already saved a bunch of time uh

play15:33

in terms of complexity time

play15:35

complexity and so that is another way

play15:38

that Haskell benefits overse that by the

play15:41

way I do want to make one quick thing uh

play15:43

by using a tree or a linked list you

play15:45

could be inefficient as compared to

play15:47

using something like a array so if you

play15:50

had some values that could be stored on

play15:52

on the stack and you had and you had a

play15:54

nice like array a contiguous memory spot

play15:57

even though you have to search in a

play15:59

linear time it can be faster than o of

play16:01

one access such as a set because a

play16:03

you're not jumping around to Crazy

play16:05

memory spots but B you're doing this

play16:07

really close locality of memory searches

play16:10

whereas it's very hard to do that with

play16:11

something like uh like a tree you can't

play16:14

I mean you could you could a heap is an

play16:16

array if you think about it so could you

play16:18

model something like that yeah you could

play16:20

you could you could Heap it hard and say

play16:22

the same thing but again now you're just

play16:24

like the argument is completely lost

play16:26

here which is choosing like of course

play16:28

course doing a quadratic search over a

play16:30

large set of items is always going to be

play16:32

slower than a logarithmic search over a

play16:34

large set of items right when I say

play16:37

large I mean n has to be sufficiently

play16:39

sized if the problem is complicated

play16:42

enough where or big enough

play16:44

where really you should the difference

play16:47

between in Access complexity in Big O

play16:48

notation complexity between a link list

play16:51

and a and a tree boom it's it's a big

play16:54

enough difference H's going to

play16:57

win but just make it a tree and see

play17:00

that's not an argument just make it a

play17:02

tree and see and it's simply a matter of

play17:05

um how I do see what he's saying though

play17:08

is that it's very simple to have this

play17:10

tree in hasal so if you program it like

play17:12

a 1D grug head then yes it's easier to

play17:16

do

play17:17

that but this is kind of like it's it's

play17:19

too much of a straw man for me to accept

play17:21

much complexity can you handle now of

play17:23

course if someone wrote a tree in C and

play17:25

you imported that library and you you

play17:26

know included it in your C code you

play17:29

would start competing again with Haskell

play17:31

right you could do that sure um but uh

play17:35

do you do that is that a possibility in

play17:38

these benchmarks it might not be a

play17:39

possibility whereas in hasell you could

play17:40

do that you could write it yourself um

play17:43

and I find that this is the case a lot

play17:44

of times in um higher level languages

play17:48

you know it's a okay I got to say it one

play17:49

more time a link list is a tree if you

play17:51

can write a linked list you could write

play17:53

a tree the end right C is very low level

play17:56

Java's higher level than but then Scala

play17:59

is higher level than Java closure is

play18:01

higher level than Java um so it's a

play18:04

spectrum and what happens is as you

play18:06

tackle these benchmarks sure C's going

play18:09

to win because it's very small problems

play18:10

right it's like you know

play18:12

calculating uh something with a known

play18:14

algorithm right so like people have been

play18:16

optimizing the C algorithm for that

play18:18

years so they know exactly how to make

play18:19

it fastest um but when you're dealing

play18:22

with more real world problems bigger

play18:23

problems uh very often you do need

play18:26

garbage collection and um higher a

play18:30

better concept of of data type um and

play18:33

data structure than c will give you and

play18:35

so that's uh what happens so just so if

play18:38

he's saying you can only rely on what's

play18:40

built into C then yeah I could

play18:42

understand that because you pretty much

play18:43

just have a raise I get that tree issue

play18:47

yeah it's not a skill issue it's a tree

play18:48

issue like I mean I understand the

play18:49

argument that hcll gives you more out of

play18:51

the box and thus you can solve a lot of

play18:53

things in a more efficient way just off

play18:56

the rip agreed I think we can all agree

play18:58

with that if that's the argument he's

play18:59

making then I think I get that argument

play19:02

right like just like with C++ how easy

play19:04

is it to make a vector it's very very

play19:07

simple the STL is is very very good

play19:10

right and so yeah does that mean C++ is

play19:13

faster than C no is it easier to get off

play19:16

the ground than C sure right can you do

play19:19

the same thing in Rust absolutely is it

play19:21

easier to get off the ground in Rust

play19:23

than C++ well you have to dye your hair

play19:25

blue first but you get the idea it can

play19:27

be easier and so it's like is that is

play19:30

that like are those Fair comparisons I

play19:32

wouldn't say they're Fair

play19:34

comparisons and it's not just about data

play19:36

structor there's other facilities of the

play19:37

language um just as a little anecdote I

play19:40

heard a story once where um there was a

play19:43

competition to see who could write the

play19:44

fastest program and it was C versus Java

play19:46

and it surprised everyone especially the

play19:48

C programmers but the Java

play19:50

implementation won and it won by a lot

play19:53

and uh the C people are like no it's not

play19:55

possible it's you know how could this

play19:57

big monstrous VM beat our highly tuned

play19:59

tiny little C program and uh they

play20:02

started reading the Java code and you

play20:04

know you can imagine them like huddled

play20:05

there with their print outs like ah then

play20:07

they all like no look they cheated and

play20:10

what they were pointing at was in Java

play20:14

they had used

play20:16

threads they used multiple threads to

play20:18

solve the problem whereas in C they

play20:23

didn't okay C11 has the new thread API

play20:27

the one before it has P threads can you

play20:29

not use just threads right like what is

play20:32

this this doesn't sound fair if you have

play20:35

a if you have a problem that could be

play20:37

massively parallelized easily then of

play20:40

course it's faster than not

play20:41

parallelizing

play20:43

it and they considered that

play20:45

cheating Well yeah if you were okay

play20:48

that's fair if you were told which

play20:50

program is faster at doing X and you

play20:53

assume that you can't use threads that

play20:55

is totally fair take go oh that's

play20:58

cheating we didn't think you could do

play21:00

that of course we could make this faster

play21:01

you want us to use threads as

play21:04

well P threads came out and who knows

play21:06

what but it was C11 right C11 has the

play21:08

new uh thread API right which apparently

play21:10

is really nice C11 uh thread uh thread

play21:13

threads

play21:15

right there's like a it's it's this new

play21:18

yeah you get you get all these like you

play21:19

get this new nice operations for very

play21:22

beautiful nice thread stuff that makes

play21:24

things a little bit easier to use uh

play21:26

which seems really really nice

play21:29

uh I have never I haven't used I I

play21:30

haven't built a lot of things in in C

play21:32

the last time I built something in C was

play21:33

a video driver uh last year and the time

play21:36

before that was a twitch IRC bot was it

play21:39

in C that was fun I liked the twitch IRC

play21:41

bot I want to do that again I want to do

play21:43

that again but let's keep on going

play21:46

so see you can see the you can see the

play21:49

the problem here right like you can see

play21:51

why they consider it cheating because

play21:53

they're used to benchmarks where it's

play21:55

just one thread purely sequential code

play21:57

and it is really hard to write threads

play22:00

in C compared to Java and in Java it's

play22:04

very easy to write threads is that

play22:07

true I I'd say in some senses probably

play22:10

true is it you don't think so I mean I

play22:13

think there's some things that are

play22:14

easier in Java to write threads for

play22:16

right like just like in Rust it's really

play22:19

easy to write a thread in

play22:21

Rust right and to do stuff and not die

play22:24

in some weird way uh but beyond that

play22:27

like

play22:29

if you're Vaguely Familiar with the p

play22:30

threads Library I feel like you should

play22:32

be able to easily do

play22:34

this Fearless concurrency I know it's

play22:37

not really a fair comparison of course

play22:39

things are easier in Java in the sense

play22:41

that you you spawn a

play22:45

thread right and you just have way less

play22:48

things you have to be concerned with

play22:49

because you have so much help from the

play22:51

garbage collection and all that but at

play22:52

the end of the day is it that much

play22:53

easier the hard part isn't in spawning

play22:55

of threads the hard part is not effing

play22:57

your yourself and creating mutexes or

play23:00

atomics that you have to constantly rely

play23:01

on to be able to do anything the hardest

play23:03

part is the paralyzation which is

play23:05

equally hard in any

play23:07

language I don't know about this I'm

play23:10

actually curi I'm most curious on uh

play23:12

low-level learnings one p thread create

play23:14

my thread boom let's go right I to me

play23:18

the paralyzation not the thread yeah

play23:20

threading seems easy it's the

play23:21

communication between threads that is

play23:23

hard do you have a non-locking cue that

play23:25

I can add stuff to are we using semop

play23:28

are we using mutases what are we using

play23:29

to control all the stuff right uh that's

play23:32

one thing I've been wanting to play more

play23:34

around with um in Rust is I always use a

play23:37

semaphore and a series of clients to

play23:39

send stuff out why not spawn Hardware

play23:41

threads and use and use non-blocking

play23:44

cues to divvy up the work and try that

play23:46

out to me that sounds actually like a

play23:48

more fun experience to do well I'd like

play23:50

to try that yeah go weight groups

play23:52

another exact same version yeah spawning

play23:54

spawning uh threadlike material in go is

play23:57

the e easiest I just go like this go

play23:59

Funk go Funk yourself so does that mean

play24:01

go is superior to

play24:04

C well I mean I would argue no and to

play24:07

start

play24:08

new and so uh you know yes what you do

play24:13

it is what do you do who won well I say

play24:16

the Java people won

play24:18

because that's the whole point of java

play24:20

is it makes those kinds of things easy

play24:21

it makes threads on you know

play24:23

crossplatform really easy uh it makes

play24:26

garbage collection crossplatform really

play24:29

easy and I think that the same thing is

play24:31

true of hll you know if it makes um

play24:35

writing a data structure that gives you

play24:38

an advantage over a link list let's say

play24:41

easy and so that's what you get that's

play24:44

what that's why it's faster all right if

play24:47

you um have some ideas about why hll is

play24:49

faster about these high level languages

play24:51

how they could be faster than a

play24:53

low-level language where you get to

play24:54

control everything and highly tune the

play24:57

you know the code down to individual

play25:00

instructions if that's the kind of thing

play25:01

you like doing how is that even possible

play25:04

um do you agree with me uh disagree I'd

play25:06

love to know you can go to lc.com

play25:09

podcast you can find ways to contact me

play25:12

email Twitter LinkedIn um you can also

play25:14

find all the past

play25:16

episodes they have audio video and

play25:19

transcripts text transcripts I I find

play25:22

his comparison completely unfair the

play25:23

first part I found actually to be the

play25:25

most

play25:26

compelling right

play25:28

the the first part was the most

play25:29

compelling meaning that it is very easy

play25:32

that there's a lot of really great

play25:34

optimizations that can be done with say

play25:37

pure functions and stuff that just makes

play25:38

it really easy to do the right thing to

play25:42

me that seems like that seems like a

play25:44

valid answer right okay you're right

play25:46

maybe it's easier to do the right thing

play25:48

in the 80%

play25:51

case true but to claim that it's faster

play25:55

is just silly I can talk about part two

play25:57

if you want but it's too hard to do in

play25:59

chat neovim runs uh into that problem

play26:01

what do you mean part

play26:02

two can you talk about part two oh

play26:05

writing your own data structures in Neo

play26:06

in

play26:07

C I mean the hard part yeah is part two

play26:10

argument writing the right data

play26:11

structure I mean the hard part about it

play26:13

is just C right you there's it's very

play26:15

you can't you don't really have generics

play26:16

you can't make a a I mean I've seen

play26:19

someone do it with macros which is

play26:21

disgusting but you can build a tree

play26:25

dedicated to a singular data type

play26:30

you know I I I mean I understand that

play26:32

it's it's harder in C right I've written

play26:34

enough C to know that it's harder and C

play26:36

to do things like being hard and C it's

play26:39

not just about generics it's probably

play26:41

about the code and reuse yeah yeah yeah

play26:42

exactly no I'm on your team I mean

play26:44

generics is what makes these a lot of

play26:46

these things more reusable but he's not

play26:48

saying it right there I know I'm on your

play26:49

team I'm fully on your team I I know I

play26:52

know the the general problems of C

play26:54

someone who I've written enough C to

play26:55

know that I'm bad at C

play26:58

right I I've written enough C to know

play27:00

that I would probably write rust faster

play27:04

generally just because I could write it

play27:06

faster and I could probably be more

play27:07

correct

play27:09

faster in general C has generics really

play27:13

you going to toss like a void star at me

play27:14

next that's the

play27:17

generic no npm andc literally unusable

play27:22

fair fair void star D nuts uh anyway

play27:31

it's not just about a code you write

play27:32

it's also about getting a code from

play27:33

other people and being able to replace

play27:35

parts of your stack yep no all that's

play27:37

completely Fair C makes it harder for

play27:39

sure 100% I am 100% the the thing is

play27:43

is it's not so the phrase how is hasal

play27:48

faster than C is just wrong what it is

play27:51

is hasal has better development

play27:53

experience than C would be the better

play27:55

thing to do would be the better thing to

play27:57

say because absolutely anything you can

play27:59

write in hll you can write in

play28:03

C

play28:05

right all

play28:06

right it's easier idiomatic hascal is

play28:10

faster than C you can't say that

play28:13

either you can't say that because I

play28:15

don't know if that's true right you have

play28:17

to show me the pro I mean you can't say

play28:22

that you can't say that idiomatic hascal

play28:25

is faster than c pig

play28:28

C that's copium that is copium idiomatic

play28:31

hascal is faster than C is

play28:34

copium dude these the the comments on

play28:37

this video on YouTube are going to

play28:38

literally just be filled with either

play28:40

hasal lovers and telling me I'm wrong or

play28:43

people going what the hell's happened

play28:46

here yeah it's not just compared to Dick

play28:48

measuring in benchmarks uh then it would

play28:50

quite valid take this is fair how is has

play28:53

fast than C once again yeah I know I

play28:55

know you're playing the devil advocate

play28:56

I'm on I I understand what you're

play28:59

saying I just don't think it's a good I

play29:01

just generally don't like those kind of

play29:03

arguments again as I made the argument

play29:05

earlier

play29:06

today which we'll pull up here comparing

play29:08

speed uh not difficulty see wins and

play29:11

speed yeah that's fair if you're saying

play29:13

it's easier to write stuff in hll I'm

play29:16

probably on your team honestly right

play29:18

look at this I even tweeted this out

play29:19

this

play29:20

morning if you do an X is faster than C

play29:23

article it only means one thing you're

play29:25

not writing C right right like this is

play29:28

100% I even pregamed this article with a

play29:32

tweet cuz I knew for a fact this is what

play29:35

was going to happen at the end of the

play29:36

article which was literally skill issue

play29:38

at the end how did I miss I I know it is

play29:41

Charo chot chot it's always that it's

play29:47

always that pre-at

play29:51

pre-at

play29:53

yeah plus typically that's not the

play29:55

question we want to ask okay so what's

play29:57

the more interesting question I think to

play29:59

ask which

play30:02

is how easy is it to write fast code to

play30:07

me that's the more interesting one and

play30:09

so something like rust makes a more

play30:11

compelling argument or something like go

play30:13

go is the 99% case it's still really

play30:18

fast but it's really easy to write and

play30:21

so to me that's like the uh what's it

play30:23

called that's the better argument to

play30:24

make which is how do you generally write

play30:26

fast code

play30:27

use a language that generally produces

play30:29

fast code Now is it going to be the

play30:32

fastest no it will not be the fastest

play30:35

but will it be fast enough that's what

play30:37

you have to ask yourself right Odin

play30:39

might win that Odin could win that Odin

play30:41

Odin's a very interesting language that

play30:43

I wish I had more time to play with I

play30:45

just wish I had more time but o camel is

play30:46

my next one right JavaScript allows you

play30:49

to write really fast in the beginning

play30:52

and then you hit what I call like the C

play30:53

of complexity where as you start writing

play30:56

you start hitting these things where

play30:57

things start falling apart in weird ways

play31:00

I think this generally is what uh he

play31:01

wanted to say except that hasal is not

play31:03

easy to write yes I know that's the

play31:05

that's that's what I'm laughing on the

play31:06

inside about which is hascal is

play31:08

historically not known to be easy to

play31:10

write like that's not one of the phrases

play31:12

you would use to describe your hascal

play31:15

experience which is easy to write that's

play31:18

just not it right like I mean even to

play31:20

get started in hll how much of your

play31:23

wardrobe do you need to replace with

play31:25

tie-dye t-shirts like that's another

play31:27

thing I don't even know if it's true

play31:28

right like how many do I need before I

play31:31

can even be remotely effective then

play31:33

someone's going to explain a monad to

play31:35

you but using a burrito and you're going

play31:37

to be so confusing so confused all right

play31:40

hey the name is I'll probably never

play31:42

learn hasal but I'm definitely going to

play31:44

learn o Camen

Rate This

5.0 / 5 (0 votes)

Étiquettes Connexes
HaskellC言語性能比較プログラミング最適化開発者言語選択アルゴリズムGC並列処理
Besoin d'un résumé en anglais ?