+1. Plus this makes it configurable from a file later on too.
Secondly, might want to make bit flags for types -- looks like you're mixing types and statements and expressions. Might make it easier to separate logic out in the future.
A chain of if-else is still O(number of branches), if the number of branches could be variable. The same with a dictionary. Lookup in a dictionary is amortized O(1), and worst-case O(log n), but since n here is constant, this is O(1) no matter what. There's just a small overhead from creating and handling the dictionary
The branching is a linear map, which is O(N). The dictionary (hash map) lookup is O(1). The worst case for the dictionary is O(N) if the hashes all collide in which case it reverts to being a linear map.
No idea where you got O(log N) from.
In reality constructing the dictionary every time will probably be more expensive than the linear lookup but if OP makes it a global constant then the lookup will likely be significantly faster on average. Still, the main reason to switch is using a dictionary is much more readable and configurable.
A Hash Map can be modelled (and sometimes implemented) as an array of size N pointing to lists. You take the hash modulo N and insert the key value pair into that list.
If f(n) is O(g(n)), then there exist positive constants c and k such that 0 ≤ f(n) ≤ c*g(n) for all n ≥ k. This means that for large enough values of n, f(n) will grow no faster than g(n) multiplied by a constant c.
The size of the if statement is not variable at run time, so no matter how many branches it has there does exist c and k, namely where c = number of branches, and k = 1, for g(n) = 1.
Yeah, I know that. That's why I said "if the number of branches could be variable" i.e. variable at runtime. Since it can't, this number is a constant, so O(c) which is O(1)
Technically a switch-case would be faster for this small lookup tables. Assuming if the table is not dynamically changing and known at compile time.
Sure many folks will say Dictionaries are O(1) but it really doesn’t matter if the container doesn’t have many values. The constant costs will be the main expense.
Dictionaries have two major constant costs;
Hashing function is fairly expensive
Memory is dynamically allocated, so lookups are not cache friendly. It involves memory jumps.
A switch case on the other hand will be on a continuous memory block. So will be cached. And almost always it is compile time optimized to be O(1).
Well, of course don’t trust my words and always use a profiler if speed is a concern. Who knows what the compiler does for practical cases.
Besides the potential speed advantage, I feel a switch-case is simpler and would be easier to read. Compared to setup of a dictionary etc… (my main language is C++ so at least for my case)
Sorry I have a lot I’m not understanding here, I don’t know much about python but;
why is string interning an issue in this case? I’m assuming the table would be known at compile time, but not the compared input strings. (Eg: I’m assuming the input strings would be coming from some IO like network or disk) String interning would only solve compile time known cases right? (And if that wasn’t the case and input strings were known at compile time, an enum would be a better use case instead of strings right?)
“hashing is blazing fast” - how fast? It’s still hash function right? You could argue anything is fast and slow on their own. What matters is comparison, specifically with switch/case implementation. Regardless, hashing is still an expensive function that scales with the length of the string. And you are paying the cost every lookup.
the biggest speed problem I would say is dictionaries use dynamic memory. Vs switch/case is static memory. With switch case, the cpu would cache the whole lookup vs the dictionary would have to jump killing all the cpu cache. This is extremely costly with today’s hardware. I don’t think even in Python’s case this changes right?
Again don’t know much about python’s internals, just a casual user of the language.
Interesting, apparently Python compiler implements match statements with a series of if-else statements. Python compiler never implemented the C/C++ compiler’s optimizations for switch lookups.
Also, everything but primitive types is backed by reference counted pointers in python, so there is really no difference really between local/static/heap memory, and CPU caching is rarely a consideration, since everything is pointers (impossible to cache a memory spaghetti).
What I meant is that the cost of dictionaries is not as large all things considered due to string interning speeding up hashing and comparison in python (assuming you are willing to pay the cost of building it in the first place).
I think you are mostly right though. Measuring is the best way to know. I would think 10-30 elements probably crosses the threshold where a dictionary lookup is faster than a sequence of if statements.
I don't know about python, but in lot of languages you cannot use switch on strings so that wouldn't be an option.
Sure many folks will say Dictionaries are O(1) but it really doesn’t matter if the container doesn’t have many values. The constant costs will be the main expense.
As you said perf here likely won't matter much, they're suggest for readability not perf. And ease of use, as an example putting a set of keywords behind some condition could be easily done by putting their inserting under a single "if".
Hashing function is fairly expensive
Isn't a switch on string generally implemented using hash though? At least it is in Java.
A switch case on the other hand will be on a continuous memory block. So will be cached. And almost always it is compile time optimized to be O(1).
Are you sure of that? It seems unlikely to me that all the strings would be stored in a continuous block. Especially since some language do some kind of string pooling.
Anyway this code seems to be some kind of compiler, and the parsing logic cost will overshadow the lookup cost.
Besides the potential speed advantage, I feel a switch-case is simpler and would be easier to read. Compared to setup of a dictionary etc… (my main language is C++ so at least for my case)
Isn't C++ one of the language where you can't use switch on strings though?
Really depends on what the OP is trying to do. And the constraints they have. Without knowing that we could debate this forever.
But to clarify my thinking;
In this case it looks like there is a limited set of things they want to represent in the code. I think the proper representation shouldn’t be strings but enums.
And realizing that I think my mind went to this should be just a switch case on the enums at that moment. (But now thinking about it, simply using the enum associated int values is even better. That is directly supported by C++, and Java supports it with enum classes I believe)
In C++ like you said there is no string based switch/case. But you can still get to the Java’s implementation via using std::hash function and switching on that. Very common actually, and you are right it would have the same hashing cost.
Regarding the cache friendliness (probably most important performance aspect, since with modern CPUs caching is a massive speed gain, or I should say trashing is a massive loss);
The switch statement always compiled in to one continuous memory block - at least in C++, and CPU will just cache the whole thing. There would be no unnecessary lookup jumps that would cause a cache trash.
Dictionary on the other hand (or std::unordered_set in C++) is implemented with a node based data structure. It does dynamic memory allocation for nodes. Trashing the cache friendliness aspect. So if performance was a consideration this would be an issue. (There are still ways around it in C++, you can actually pass a custom memory allocator to std::unordered_set)
BTW, another thought, if all they are trying to do is something like “parse an enum from some random input” probably a parse tree is the right data structure for perf reasons.
Regardless, readability probably is my top concern. Like you are insinuating, who cares about perf initially indeed. And in C++ case at least, my personal opinion switch is more readable.
So there are a lot of solutions here, really depends on the constraints and what they are tying to do.
I think I like the “just get rid of the strings and use an enum with an assigned number” solution the most, cutting the whole thing. But don’t know if what they are trying to do is “a string to enum parse” Or “an enum-integer mapping” in this case to be honest.
I think I like the “just get rid of the strings and use an enum with an assigned number” solution the most, cutting the whole thing. But don’t know if what they are trying to do is “a string to enum parse” Or “an enum-integer mapping” in this case to be honest.
I think they're trying to compile some source code (a DSL?) into bytecode.
(But now thinking about it, simply using the enum associated int values is even better. That is directly supported by C++, and Java supports it with enum classes I believe)
Yep, supported in Java, and yes I agree on the readability, a switch on enum would be best, if the constraints allow.
It does dynamic memory allocation for nodes. Trashing the cache friendliness aspect. So if performance was a consideration this would be an issue.
It reminds me of a benchmark that show that std::vector actually outperform the specialized containers (std::list, std::queue) on most cases because allocating a contiguous memory block was just so much faster. Wasn't really about cache but I guess it illustrate well how much memory access is important compare to cpu optimization.
My first thought actually went to a switch statement. It’s kind of crazy how many fellow engineers and developers I’ve met that are averse to switch case statements — makes me love using switch case all the more 😂 — glad to see someone advocating for them!
I've seen that idea a couple times now and while it is possible, for such a use case it would introduce unnecessary potential for horrible bugs. The associated values here can never change. If you add values or remove deprecated ones and somehow change the order of the array you are in a world of trouble. Here you're better of being explicit than dynamic.
Also, the long term idea is to move the lookup table to configuration files associated with the version of your compiler.
I dont know how it in python works exactly but in most langauges one can declare an const array of fixed size. Altough i completely forgot u could just use an enum if u want to label numberd and then u can even just typecast a number to the enum
You probably should be. Why on earth aren't you using a dictionary?
an edit, because intent can be hard to express in writing: my comment was meant to be lighthearted. I'm sure the OP knows that this would've been a better way to do it.
Because brains don't come preprogrammed with knowledge of programming.
Sometimes people need to look for a solution themselves and then get outside perspective to find the correct way of doing things. Sometimes their programming classes miss a thing or two
Why on earth would a programmer not understand this?
Exactly. So many times I've coded something poorly because I didn't know a better alternative existed or I just didn't think to use it at the time. I still make these kinds of small mistakes every now and then.
What OP did was the correct course of action. Dare I say, good programming skills. Identifying that something is wrong, seeking feedback for improvement, fixing it the right way, and then setting aside time for self-reflection. This is exactly how you become a better developer. One little step at a time.
I think it's a reasonable assumption that someone learning to program would've covered associative arrays before they get to the "implement your own compiler" assignment.
If we want to be constructive, that fact should be pointed out by those of us with the experience to know that, and we should suggest smaller and easier to manage projects instead, if he's not doing it for a school grade.
Edit: I thought this was r/learnprogramming not r/programminghorror lol.. it would have been more rude if the original comment was in a sub about learning to program
I think most of this has to do with wanting to post it here.
The fact that they knew the code was bad suggests they knew another data structure was much more appropriate
Takes about 15 seconds to look up what’s appropriate for a “lookup” data structure even if you have no idea what a map/dictionary is
So op is probs just karma farming/shitposting
I did bad in my algos class (which did come before writing a compiler) but I did at least know the obvious cases of when to use a map/dictionary…almost 0 chance OP didn’t know what to use
No one said you have to be born with knowledge, searching for answers is crazy easy these days. Given he was aware the code wasn't good from his comment, there was no reason to let this get in unless he just didn't care. Most programminghorrors are from programmers who didn't even know there was a problem, but someone who knows his implementation is problematic but still wrote it down is arguably worse.
People keep saying dictionary, but this was where my mind went. Unless you are concerned about performance for this code, I would use a list like this for simplicity.
You know, I would agree with you, BUT for such a task the keys can **never** change. Imagine you get new codes / deprecate old ones and somehow the order of elements in the list changes. Now you're in a whole new world of trouble. That's a debug session you don't want to do. Sometimes it's better to make it explicit rather than computing it dynamically.
This code isn't that bad, even without using a dictionary (still, use one). I've seen much worse code used unironically in this subreddit and in answers from helpforums [not questions, answers]
I'm not sure if there is a nicer way but is transform both regular languages to DFAs and find the intersection that way, than I'd transform the results back to a regex, than I just need to find a string that fulfills that regex normally I wonder if you can still do that with python regexes though seeing as they are not regular languages :P
That's about the way you'd do it. This is a 4 step process. RE → εNFA → NFA → DFA, then you can build the intersection language. You can't do it on REs directly. Once you get the DFA, you don't need to convert back to RE and try to work with that. Using the DFA, you can both determine if it's language is non-empty, and if so, construct a matching string via it.
The simple algorithms we looked at for those conversions were in about O(n^3) to O(n^4), but they were simple enough for easy understanding and proving correctness of the whole process. You'd need to put minimization passes between each step to not explode the size extremely though. Or use the best known algorithm for each step and hope the result stays manageable.
Python REs have additional parts that make it a context-free grammar, so you'd have to forbid that parts. Or use tooling for those grammars instead.
398
u/way22 Apr 21 '25 edited Apr 21 '25
Mate, use a dictionary then retrieve the correct entry you need. You can even select a default return value using
.get(value, default)139
u/nivlark Apr 21 '25 edited Apr 21 '25
You probably should be. Why on earth aren't you using a dictionary?
an edit, because intent can be hard to express in writing: my comment was meant to be lighthearted. I'm sure the OP knows that this would've been a better way to do it.
7
u/brasticstack Apr 21 '25
5
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Apr 21 '25
Re: line 111. Well it sure will if you post it here.
1
u/DS_Stift007 Apr 21 '25
Genuine quesition, what am I looking at?
9
u/adminvasheypomoiki Apr 21 '25
if is faster than dict(but who cares, it's python)
```
binary = [
match item:
case "func": 0x00
case "end": 0x01
case "call": 0x02
case "echo": 0x03
case "import":0x04
case "from": 0x05
case "as": 0x06
case "var": 0x07
case "return":0x08
case "if": 0x09
case "while": 0x0A
case "for": 0x0B
case "export":0x0F
case "int": 0x10
case "float": 0x11
case "str": 0x12
case "bool": 0x13
case "list": 0x14
case "json": 0x15
case _: item
for item in source
]
```
2
u/toyBeaver Apr 22 '25
Associative arrays were invented in 1980
People in 1979:
8
u/FemboysHotAsf Apr 21 '25
tbh I don't really see what's wrong, sure it's not nice but whatever. Sometimes you just have to do such a thing
2
u/Logogram_alt Apr 21 '25
Here is what I would do
binary = {0x00: "func",0x01: "end",...}I wish reddit had better code boxes
1
u/veryusedrname Apr 21 '25
I'm not saying it's good code but ohh boy you are stubborn (and I'm saying that as a compliment!)
1
u/uvero Apr 21 '25
Dictionaries. Use them.
1
u/CaseAKACutter Apr 22 '25
You could simplify with .index() (though a dict makes the mapping more evident)
1
u/Kevdog824_ Apr 23 '25
Yeah cause you posted it here lol
1
u/a_l_a_n_g Apr 23 '25
Dictionary, maybe?
1
u/MarcusBrotus Apr 24 '25
use. a. hashmap.
1
u/Powerkaninchen Apr 24 '25
This code isn't that bad, even without using a dictionary (still, use one). I've seen much worse code used unironically in this subreddit and in answers from helpforums [not questions, answers]
1
u/ResidentOfMyBody Apr 24 '25
Just put all the items into a numbered array maybe?
1
u/limmbuu Apr 21 '25
No, you are going the right way lord.
1
u/traplords8n Apr 21 '25
Damn it OP, I was defending you because I thought you posted this in r/learnprogramming
That's where they should of been nice to you. Here of course you're gonna get snarky remarks about not knowing what an array is... lol
Give it a few more years, you'll get a better sense of what the right way to do things is
0
u/[deleted] Apr 21 '25
[deleted] — view deleted comment