Hot answers tagged

51

Performance [..] winforms is not powerful so it takes 1-2 seconds to refresh the content [..] It's not WinForms because it's actually very fast and I have never had any issues with it. There is a 99.99% chance that the code is inefficient so let's have a look. Expression.Compile() This is what slows the application down and where the bottle-neck is ...


37

Some of the numbers might seem plucked out of thin air but they aren't Still better have them explained. I can understand 3.6 and even 19.6 (it is \$2g\$, isn't it?), but I have no idea what 98/490 stands for. Declare them as symbolic constants with meaningful names. I don't see a need for abs in reaction_time = round(abs(diff.total_seconds()), 2) ...


21

(relatively critical review ... apologies in advance) The two issues I feel are most incorrect about your code is the buggy switch statement, and the poor choice of timing mechanism. The decision to use these mechanisms has lead to a poor OOP design. Switch First, though, the switch bug: switch (timerChoice) { case 1: friendlyBlue(); case 2: ...


18

I suggest breaking the code into functions, so you can easily import your code into a Python shell and test that your computation is exact (and not just thinking it is). To expand on other answers, you may: retry asking the speed if it is not an integer (detected when the int function raises a ValueError); in the same vein, since you don't really need to ...


15

First thing I would do is change if (mainString.Length == desiredLength) to if (mainString.Length >= desiredLength), since if it's longer it'll just do excess work with the StringBuilder and you may as well return early on all conditions that would satisfy it. I would also combine this try/catch block: try { //ensures process wont deny access if ...


15

I wrote a simple main() function to test this: int main() { show_progress_bar(100, "progress", '#'); } I immediately noticed that I only saw a couple of updates. This is because the output is not flushed after each update. Whether you see the same effect depends greatly on your output device (I was using Emacs compilation-mode, which might have more ...


14

I spent some time last night reviewing this code. Here are some specific suggestions I can make: Move your game loop into an if __name__= "__main__": statement at the bottom of the code. Currently, if the module is imported, the person who imports it will immediately start playing chess, which isn't desirable (they may just want to know about the piece ...


13

It crashes if user input for speed_ms is not integer If user presses any key before t = input("GO!! ") his reaction time is saved as 0. This is a major flaw in a code, and should be fixed.


13

Keep solid picture of usage in your head When creating a library, it is extremely important to always keep the intended usage in mind. If the usage syntax is disgusting, it is better to think of a new one before writing the library (of course within reasons). In this case, I believe it is not what you wanted. Make interfaces easy to use correctly and hard ...


12

You could pull out the while loop of each function and make it its own function. Something like this private static void Countdown(long startTime, long endTime, string finalMsg){ while (System.currentTimeMillis() / 1000 < endTime) { while (startTime != System.currentTimeMillis() / 1000) { startTime += 1; if (endTime - ...


12

Use the standard library One of the things that immediately jumps out at me is this: struct split { //linked list to store data std::chrono::microseconds elapsed; split *next; }; split *head; You don't have a destructor or copy constructor provided, which means you're leaking memory. Also, you're making the whole problem harder on yourself since the ...


12

If it's wet, the friction_coefficient will be lower. Which means that Bd will be lower (which is probably wrong). It's difficult to see that, because of all the unnecessary parentheses. You missed the necessary ones. You probably want Bd = speed_ms**2 / (19.6 * friction_coefficient) Rt, Rd, and Bd should get better names, or need a comment. In particular ...


12

player_1 is read and assigned but never used. If the user misspells wet (e.g. as Wet or as wet), the program assumes that dry was meant - it should ask again until it gets an answer it understands or the player gives up (and consider adding icy for more fun). There's a lot of unnecessary parentheses - they could be trimmed to make the code more readable.


11

First: I think the point @DFord makes is quite good. Second, I think you should work on indenting your code better. In particular, the indentation of your main if/then/else chain is misleading: if (endTime - startTime > 1) if (endTime - startTime == 360) System.out.println("6 Minutes left"); if (endTime - ...


11

You want to be careful with setInterval() and setTimeout(), because javascript runs in only a single thread if the event loop is busy it won't fire the code in setInterval() or setTimeout() exactly as expected. This means your timer could take longer than you expected. Instead of starting with a counter and decrementing on each interval, you can ...


11

According to the javadoc, a Calendar instance is initialized with the current date and time. So instead of this: Date date = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int hour = calendar.get(Calendar.HOUR_OF_DAY); You don't need to create a separate Date object, so you can simplify like this for the same effect: ...


11

Spacing First of all, yourcodeispackedverytightly, try using spaces a bit more. This is preferred: timer = (System.currentTimeMillis() - time) / 1000; int number = rand.nextInt((max - min) + 1) + min; Static variables private static long time; private static long gameTime; I see absolutely no need to have these as static. As it is right now, your code ...


10

Is this timer efficient? Yes, but there are two optimizations that I can see. Get rid of the goto and use a do-while loop. I have noticed a slight performance increase when it has been written this way in other programs. This increase will become non-existent however if you have compiler optimization settings enabled. If they are enabled, they compile ...


10

Concept: Your concept is not suited to be used on multiple countdowns, instead you might want to try something like the following: Beware, this is a raw draft! public class Buff{ private int respawnSeconds; private String name; private int respawnsAtSecond; //constructor + getters and setters public void kill(int currentGameSeconds){ ...


10

Hmmm.... this is a big one. Right, let's break this review in to five sections: Code Style Threads Class Hierarchy Swing Performance.... Code Style This is a laundry-list of things. I'm not going to explain them all, but: Class names should be CamelCase, lolee is not a good name. Nor is class keyPressed. Class names like Action1 etc. are also useless for ...


10

The primary problem I see here is the way we're keeping track of the time passed. NSTimer is not guaranteed to tick with the exact amount of time you passed it. It will try getting as close as possible, but the most specific problem is when the thread has a lot of activity on it. If between two ticks, the thread the timer is one gets a lot of activity ...


10

Ok, had a few suggestions with the code for you to avoid some confusion and maintain some consistency. I'll start off with the code sample and add some comments after the code block: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using ...


10

Typically user-defined types are named with a leading capital, i.e. Times, Split, etc. You should put your interface in a header (hpp) file, and your implementation in a cpp file. I like to explicitly label the private sections of my classes. Unlike in C, when you have a pointer they are traditionally declared as Type* name not Type *name, but this is a ...


10

matplotlib adding stuff to the current figure is because you are not using the OO-interface. It is slightly clunkier, but allows way more freedom In my view, you are mixing several things up. I would seperate the generation of the timings, the aggregation of these results and the plotting. In this way, if you want to change to another plotting library (...


10

First off, the obligatory recommendation that you use System.Diagnostics.StopWatch for this purpose, and not DateTime.Now (or even UtcNow, which won't go wrong if you happen to enter daylight savings while the program is running). Using Diagnostics.Stopwatch is more precise than the methods in DateTime, and it provides an Elapsed property which returns a ...


10

Good effort. A few small points, with lots of links to my blog along the way. namespace Stopwatch { public class Stopwatch { Never name a class the same as its namespace. A world of pain awaits those who do. My four part series of articles describing that world of pain begins here. private DateTime _startDate; There's no need to underbar ...


10

There are a couple of things which could be more consistent. public static readonly DependencyProperty StartAngleProperty = DependencyProperty.Register("StartAngle", typeof(double), typeof(Arc), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender)); public static readonly DependencyProperty ...


9

In Python 3.3 and later you can use one of time.perf_counter() Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to measure a short duration. It does include time elapsed during sleep and is system-wide. The reference point of the returned value is undefined, so that only the ...


9

The problem with any sort of timer in which each "tick" is a measurement from the previous "tick" is that in software, these "ticks" are not guaranteed to be exact. This means that if your first tick is off by a tenth of a second, every remaining tick will be off by that amount or more. Your second tick is scheduled based on your first tick. This means ...


9

To generate those nested, nearly identical setTimeout calls, instead of coding them all manually you could have a recursive function that builds and returns a function for you. Something like this would work: // Accepts an array of elements and a final callback function // Returns a function to show all elements and execute the callback function ...


Only top voted, non community-wiki answers of a minimum length are eligible