+ 3

Can you teach me if this is good or not

Is this good or is if-else more good https://sololearn.com/compiler-playground/c6P8VQL0oTT4/?ref=app

16th Oct 2024, 8:55 PM
Sharpneli
Sharpneli - avatar
13 Respostas
+ 5
I like what you did. Mainly I share this as some alternatives that you could consider, but are not necessary changes. 1. Declare the constants inside the class. These constants only apply to the class, so they should be defined inside the class for better encapsulation. While you can declare them individually, it's slightly cleaner to make it an ENUM. Also, constants should be in all caps. Here is how I did them: class User { public: enum Privilege { IS_AUTH = 0b00000001, IS_ADMIN = 0b00000010, IS_MOD = 0b00000100 }; 2. Within the class, the test functions (isAdmin, isAuth, isMod) all compare the permission to hard coded numbers, 1, 2, and 4. Replace the 1, 2, 4 with the constants. This way you avoid hard coded values where you already have constants that handle those values. Here is how I wrote that part: inline bool isAuthenticated() const { return level_ & IS_AUTH; } 3. Finally, I created a timing function to measure execution time of your methods. I invoke those methods 10000 times and print the execution times. Try it. Run this as is. Then remove "inline" and run it again. If you want to do other tests, you can use this method to measure execution times. Run each version several times and see if there is a difference. In this case, using "inline" seems to give a slight improvement on execution time. Take out "inline" and try it again several times and see for yourself. NOTE: This method of performance measurement is impacted by CPU time, etc. So you need to do the test several times to know if it's typically faster or not. My full version of your code with those changes is here: https://www.sololearn.com/en/compiler-playground/c6Udy9Oyttjj
16th Oct 2024, 9:55 PM
Jerry Hobby
Jerry Hobby - avatar
+ 5
Here is the code I wrote: // Created by Sharpneli // modified by Jerry Hobby to demonstrate some minor improvements. #include <iostream> #include <chrono> #include <vector> using namespace std; class User { public: enum Privilege { IS_AUTH = 0b00000001, IS_ADMIN = 0b00000010, IS_MOD = 0b00000100 }; User(const char* uname, int privilege) : level_(bit_) { level_ |= privilege; } inline bool isAuthenticated() const { return level_ & IS_AUTH; } inline bool isMod() const { return level_ & IS_MOD; } inline bool isAdmin() const { return level_ & IS_ADMIN; } void toggleMod() { level_ ^= IS_MOD; } private: static constexpr int bit_ = 0b00000000; int level_; }; void measure_execution_time(User& user) { int test_runs = 10000; vector<double> durations; durations.reserve(test_runs); for (int i = 0; i < test_runs; ++i) { auto start = chrono::high_resolution_clock::now(); user.isAuthenticated(); user.isMod(); user.isAdmin(); auto end = chrono::high_resolution_clock::now(); chrono::duration<double> duration = end - start; durations.push_back(duration.count()); } double total_duration = 0; for (double d : durations) { total_duration += d; } double average_duration = total_duration / durations.size(); cout << "Ran " << test_runs << " iterations." << endl; cout << "Average execution time per iteration: " << average_duration << " seconds." << endl; } int main() { User u {"Sharpneli", User::IS_AUTH | User::IS_ADMIN | User::IS_MOD}; User a {"nyaki", User::IS_AUTH }; // measure performance of the functions measure_execution_time(u); boolalpha(cout); cout << u.isAuthenticated() << endl; cout << u.isMod() << endl; u.toggleMod(); cout << u.isAuthenticated() << endl; cout << u.isMod() << endl; return 0; }
16th Oct 2024, 10:34 PM
Jerry Hobby
Jerry Hobby - avatar
+ 5
+1 for Jerry Hobby suggestions. Encapsulating enums makes it more self-contained and adds additional constraint check on what can be passed to the constructor. I added a few more toggles and a prettified showStatus for easier status inspection. Also changed the ordering a bit. Maybe Admin should be higher ranked than Mod? https://sololearn.com/compiler-playground/c037q9fEfGba/?ref=app
17th Oct 2024, 6:05 AM
Bob_Li
Bob_Li - avatar
+ 4
Library functions can sometimes add additional memory requirements to your code. If you are really concerned about memory footprint, you can write the functions yourself instead of using library functions or you can use built-in or library functions that are less demanding. An example, in C, is that printf() is much more bloated than puts(). Printf has all the code for formatted strings, etc. So it requires more memory. In order to test this, try to remove any library function and recompile. See how much memory it takes. If it's "fat", then look for alternatives. Also, setting up functions and/or objects could also have some overhead. You could write a very streamlined bit of code that uses less memory by using preprocessor macros, write efficient arrays, and avoid using prebuilt functions. Which ones take more memory is beyond what I know. I would look at replacing cout and objects and make it as minimal as possible. Once I had to store a larger amount of data in as small of space as possible. For that I used unions to map every data object into as tiny of a space as possible. I calculated how many bits were required for each data element and built a union to map those. I was able to store a great deal of data in a tiny space on disk. In my case, I had to store many thousands of records and the work to "compress" everything this way reduced my storage demands by about half. I did all this in C. I am not sure what the best technique for C++ would be. In my case, every bit counted. It's hard to advise the particulars here as we don't know you precise requirements. But the point is, you can go over every aspect of your code and consider what's absolutely necessary and make sure everything you do has a minimal impact.
17th Oct 2024, 5:26 PM
Jerry Hobby
Jerry Hobby - avatar
+ 3
Thank you Jerry hobby, but I cannot open the link because it return not allowed in duckduckgo browser. It is true I should use enum but I will try it, before I want to use std::bitset but I think it take memory for the 90kb hardware my work give me. If enum is good and fast I will use it. I will try now
16th Oct 2024, 10:24 PM
Sharpneli
Sharpneli - avatar
+ 3
Yes admin is high than others. When I try Bob_Li it was 4kb and Jerry Hobby is 3kb. Is it because string_view? I still have much space on the chip so it is not matter. Both are good but if I remove string_view both of it are 3kb The leader ask me to use minimum space complexity
17th Oct 2024, 9:03 AM
Sharpneli
Sharpneli - avatar
+ 3
Jerry Hobby's idea of using C instead of C++ sounds good. This is my try at translating the code to C. Perhaps it could be optimized further? https://sololearn.com/compiler-playground/c966MrYiAyxx/?ref=app
18th Oct 2024, 3:56 AM
Bob_Li
Bob_Li - avatar
+ 1
Hi
18th Oct 2024, 2:32 AM
Mohammed Arman
Mohammed Arman - avatar