Benchmarking your code
Benchmarking your code is very important since it allows you to pinpoint bottlenecks in your application and allows you to improve the overall performance of your software as well as learning which techniques are more taxing than others.
For this example we are going to be using the Stopwatch
class. This class will enable us to see how many milliseconds it required for our code to be executed so we can compare different methods or techniques.
As an example, the code below checks the performance difference of a try/catch block in a loop vs the performance when not using one. Keep in mind that try/catch blocks are not that performance heavy if they don’t actually catch something but the performance gets hit a lot in cases a lot of exceptions are being thrown.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | Stopwatch sw = new Stopwatch(); int test; //test 1 sw.Start(); test = 0; for (int i = 0; i < int.MaxValue; i++) { test = i; } Console.WriteLine(sw.ElapsedMilliseconds); // 7644 sw.Reset(); //test 2 sw.Start(); test = 0; for (int i = 0; i < int.MaxValue; i++) { try { test = i; } catch { } } Console.WriteLine(sw.ElapsedMilliseconds); // 8429 sw.Reset(); |
For even better results I would advise running each test 3 to 5 times and simply calculating the average time of execution.
Move JavaScript files to footer in WordPress
Loading excessive JavaScript files in the header will usually slow down your website. This happens because while JavaScript files are loading the browser stops all the other operations till those files have been processed.
An easily solution to remedy that is to move all the .js files in the footer so they will start to load after all the graphical elements of the website have been loaded. In order to do this, open your theme’s functions.php file an place the following code at the bottom.
1 2 3 4 5 6 | remove_action('wp_head', 'wp_print_scripts'); remove_action('wp_head', 'wp_print_head_scripts', 9); remove_action('wp_head', 'wp_enqueue_scripts', 1); add_action('wp_footer', 'wp_print_scripts', 5); add_action('wp_footer', 'wp_enqueue_scripts', 5); add_action('wp_footer', 'wp_print_head_scripts', 5); |
Note: Keep in mind that in some cases some plugins might rely on specific JavaScript files to be loaded in the header, so make sure to check that everything is working properly (no errors in the java console) after moving all the files in the footer.
Posted in WordPress.
Tagged javascript, WordPress
Minimize a form without border using the taskbar
By default borderless forms are not designed to be minimized, which means when the form’s FormBorderStyle
property is set to None you will notice that clicking the application box in taskbar does not minimize the form.
This can be fixed by overriding CreateParams
and adding the WS_MINIMIZEBOX
style to the Window and CS_DBLCLKS
to the Window class styles.
Simply place the following code inside your Form’s class which you want to enable the minimize functionality using the taskbar.
1 2 3 4 5 6 7 8 9 10 11 12 13 | const int WS_MINIMIZEBOX = 0x20000; const int CS_DBLCLKS = 0x8; protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.Style |= WS_MINIMIZEBOX; cp.ClassStyle |= CS_DBLCLKS; return cp; } } |
Posted in C#.
Tagged C#, CS_DBLCLKS, csharp, FormBorderStyle, snippet, winforms, WS_MINIMIZEBOX