Thread Rating:
  • 2 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5

[Guide] Pointers and Strings

#3
(March 12th, 2020 at 2:59 PM)Darth-Apple Wrote: I read through. I was waiting the entire time for you to put "int len = " and to use the for loop with the variable instead of the full statement. And I saw at the end. You so, so made me wait for it. Finna

Honestly, I see a lot of programmers make that particular mistake. What they don't realize is that in many languages, len(some string) is an O(n) operation. A single "for (i = 0; i < strlen(string); i++)" operation becomes an O(n^2) operation just like that.

I'm glad that there is a surge of programmers (along with resources to get them started). The science of things like this is anything but beginner friendly, but at some point, however, it becomes necessary to know. Things like this are exactly why a very solid backing in the inner-workings of low-level code are so important.

Also, I'm very surprised that the OS takes things a single byte at a time. On one hand, operations that involve computations on a single byte have no choice. (32 or 64 bits does not mean 4 or 8 operations. Just that one operation is 32 or 64 bits. And a simple "is this character 'x'" operation is an 8 bit operation fundamentally.) I suppose I'm always a little slow to use string operations for that reason, but when I do use them, I use the available tools. Regex is great (still doesn't make total sense to me, but I have the hang of it enough to know my way around it).

But copying/writing strings is something where I'm surprised on that one. I suppose there is a reason for this too, or perhaps it's convention also (much like 75% of the C++ language. Tongue)

--------------------------------------------------
Regarding 1.9MB Javascript libraries...
I couldn't agree more. Not only with javascript (because the browser has to load these and that takes time), but in principle, just in general. If I can do something WITHOUT a framework, why not? Seriously. 90% of the time, it's not hard to do without. (You already have to learn the base language. With a framework, you have to learn the base language AND the framework. It's like learning two languages to do one thing.)

That's something I LOVE about MyBB. Yes, they use Jquery (I'm okay with that, jquery makes javascript much easier), but they don't use symfony or any other PHP libraries. The pages are in actual .php files. It's not modernized at all, but it's so, so much cleaner and easier to code for. Unfortunately, the world is moving towards more bloated approaches, and people don't appreciate the simplicity of old ways like they used to.

Anyway, thanks for writing this! I really like your post, and learned a lot just reading through it. Smile

One note: I'm pretty sure C's standard-library implementation strlen() is O(nlogn) or something, because strlen itself doesn't actually iterate byte-by-byte.
I remember reading an article on it a while back, it 'accelerates' while trying to read how long the data-type is, which drastically increases performance for larger data types (but not so much smaller ones.)

You'd think you could make a simple implementation of the same function using a single loop or, like four lines of code, but the actual glibc implementation is closer to 25 SLOC (that's without all the comments) as a result.
I've been watching musl-libc as well because I was interested in using VoidLinux a while ago (which takes pride in using musl over glibc for the sake of being lightweight), and musl-libc basically tries to make a tinier glibc while trying to keep most of the functionality and optimizations. It's pretty cool, and their implementation is like 22 lines as well (including other includes and defines)

But either way, calling it n times isn't exactly great either.

I considered using <regex> for this because I've used it in the past for other C++ projects, but sadly that's just more bloat, and it's been known to compile badly on linux machines under gcc/g++. Thus I stayed away from it.
Reading/writing data is pretty cool when you study some assembly to see how it's actually done under the hood, and makes sense considering that the CPU only really has access to one memory address at a time, which should only have the size of one byte.Thus, like I said, RDX can be used if you're specifying multiple bytes to read/write/store, but essentially the syscall just makes a loop to do that.

Loop unrolling makes you think that everything's f*** when it comes to optimizations. I hate it.
But on the other hand my CPU should be able to support 2,700,000,000--3,500,000,000 operations per second per core so f*** it, I can spare a couple extra instructions here and there at the sake of not spending hours writing this haha.
Reply


Messages In This Thread
[Guide] Pointers and Strings - by Lain - March 12th, 2020 at 5:02 AM
RE: [Guide] Pointers and Strings - by Darth-Apple - March 12th, 2020 at 2:59 PM
RE: [Guide] Pointers and Strings - by Lain - March 12th, 2020 at 11:13 PM
RE: [Guide] Pointers and Strings - by Darth-Apple - March 20th, 2020 at 12:12 AM



Users browsing this thread: 2 Guest(s)

Dark/Light Theme Selector

Contact Us | Makestation | Return to Top | Lite (Archive) Mode | RSS Syndication 
Proudly powered by MyBB 1.8, © 2002-2024
Forum design by Makestation Team © 2013-2024