{"id":12,"date":"2008-12-21T08:33:26","date_gmt":"2008-12-21T13:33:26","guid":{"rendered":"http:\/\/www.stephencalenderblog.com\/?p=12"},"modified":"2021-12-31T18:28:09","modified_gmt":"2021-12-31T23:28:09","slug":"benchmarking-operation-speed","status":"publish","type":"post","link":"https:\/\/www.stephencalenderblog.com\/?p=12","title":{"rendered":"Benchmarking: Operation Speed"},"content":{"rendered":"<p>The fundamental speed hierarchy, bit shifting &lt; addition &lt; multiplication &lt; division, is true for all languages.  However, it is still important to know the speed difference between operations.  Furthermore, the basic operations have many different flavors in which they can be written.  All of these trials compare basic operation speed, alternative syntax, and mathematical substitutions.<!--more--><\/p>\n<p>Comparing verbose notation to assignment notation, 1,000,000 computations per trial.  Run the benchmarks: <a href=\"http:\/\/stephencalenderblog.com\/becnhmarks\/BenchmarkVerbose.php\">verbose notation<\/a>, <a href=\"http:\/\/stephencalenderblog.com\/becnhmarks\/BenchmarkAssignment.php\">assignment notation<\/a><\/p>\n<table cellspacing=\"0\" cellpadding=\"4\" width=\"470\" rules=\"groups\">\n<tbody><\/tbody>\n<colgroup span=\"0\" style=\"color:#000000;\"><\/colgroup>\n<colgroup>\n<col align=\"center\">\n<col align=\"left\">\n<col align=\"center\">\n<col align=\"right\">\n<col align=\"right\">\n<\/colgroup>\n<tbody>\n<tr>\n<th width=\"90\" align=\"left\">&nbsp;<\/th>\n<th width=\"120\" align=\"left\" bgcolor=\"#D8D8D8\">Division<\/th>\n<th width=\"130\" align=\"center\">Relative Speed<\/th>\n<th width=\"60\" align=\"right\" bgcolor=\"#D8D8D8\">Average<\/th>\n<th width=\"70\" align=\"right\">Std. Dev.<\/th>\n<\/tr>\n<\/tbody>\n<colgroup style=\"color:#000000;\">\n<\/colgroup>\n<tbody>\n<tr>\n<td rowspan=\"2\">Computer A<\/td>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = a \/ b<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.1791<\/td>\n<td align=\"right\">0.0094<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a \/= b<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.1762<\/td>\n<td align=\"right\">0.0122<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table cellspacing=\"0\" cellpadding=\"4\" width=\"470\" rules=\"groups\">\n<tbody><\/tbody>\n<colgroup span=\"0\" style=\"color:#000000;\"><\/colgroup>\n<colgroup>\n<col align=\"center\">\n<col align=\"left\">\n<col align=\"center\">\n<col align=\"right\">\n<col align=\"right\">\n<\/colgroup>\n<tbody>\n<tr>\n<th width=\"90\" align=\"left\">&nbsp;<\/th>\n<th width=\"120\" align=\"left\" bgcolor=\"#D8D8D8\">Multiplication<\/th>\n<th width=\"130\" align=\"center\">Relative Speed<\/th>\n<th width=\"60\" align=\"right\" bgcolor=\"#D8D8D8\">Average<\/th>\n<th width=\"70\" align=\"right\">Std. Dev.<\/th>\n<\/tr>\n<\/tbody>\n<colgroup style=\"color:#000000;\">\n<\/colgroup>\n<tbody>\n<tr>\n<td rowspan=\"2\">Computer A<\/td>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = a * b<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.1645<\/td>\n<td align=\"right\">0.0080<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a *= b<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.1603<\/td>\n<td align=\"right\">0.0071<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table cellspacing=\"0\" cellpadding=\"4\" width=\"470\" rules=\"groups\">\n<tbody><\/tbody>\n<colgroup span=\"0\" style=\"color:#000000;\"><\/colgroup>\n<colgroup>\n<col align=\"center\">\n<col align=\"left\">\n<col align=\"center\">\n<col align=\"right\">\n<col align=\"right\">\n<\/colgroup>\n<tbody>\n<tr>\n<th width=\"90\" align=\"left\">&nbsp;<\/th>\n<th width=\"120\" align=\"left\" bgcolor=\"#D8D8D8\">Addition<\/th>\n<th width=\"130\" align=\"center\">Relative Speed<\/th>\n<th width=\"60\" align=\"right\" bgcolor=\"#D8D8D8\">Average<\/th>\n<th width=\"70\" align=\"right\">Std. Dev.<\/th>\n<\/tr>\n<\/tbody>\n<colgroup style=\"color:#000000;\">\n<\/colgroup>\n<tbody>\n<tr>\n<td rowspan=\"2\">Computer A<\/td>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = a + b<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.1571<\/td>\n<td align=\"right\">0.0040<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a += b<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.1603<\/td>\n<td align=\"right\">0.0092<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>I always assumed that the short hand assignment would be faster since it has one less variable reference.  While there is a slight difference in speed, it is less than the standard deviation and all trials have the same maximum.  This was true across all computers.  The reason that these trials are 10 times slower than the following trials is that the loop is calling a function to avoid boundary issues that would happen with dividing or multiplying a number 1,000,000 times.  Division is so costly because of all of the decimal remainders that are calculated for every computation, similarly addition and multiplication do get slower for larger numbers.  All of these calculations were with the same 2 digit numbers to have a constant operation speed and isolate speed differences between syntax.  Conceivably, there could be a difference in speed dwarfed by the expense of a function call, but that difference would be small enough to treat as insignificant.<\/p>\n<p>Comparing the speed of division, multiplication and bit shifting, 10,000,000 computations per trial. Run the benchmarks: <a href=\"http:\/\/stephencalenderblog.com\/becnhmarks\/BenchmarkOperationsInteger.php\">operations with integers<\/a>, <a href=\"http:\/\/stephencalenderblog.com\/becnhmarks\/BenchmarkOperationsNumber.php\">operations with numbers<\/a>, <a href=\"http:\/\/stephencalenderblog.com\/becnhmarks\/BenchmarkOperationsCasting.php\">operations using numbers and casting<\/a><\/p>\n<table cellspacing=\"0\" cellpadding=\"4\" width=\"470\" rules=\"groups\">\n<tbody>\n<tr>\n<th width=\"80\" align=\"left\">&nbsp;<\/th>\n<th width=\"140\" align=\"left\" bgcolor=\"#D8D8D8\">Division<\/th>\n<th width=\"120\" align=\"center\">Relative Speed<\/th>\n<th width=\"60\" align=\"right\" bgcolor=\"#D8D8D8\">Average<\/th>\n<th width=\"70\" align=\"right\">Std. Dev.<\/th>\n<\/tr>\n<\/tbody>\n<colgroup span=\"0\" style=\"color:#000000;\"><\/colgroup>\n<colgroup>\n<col align=\"center\">\n<col align=\"left\">\n<col align=\"center\">\n<col align=\"right\">\n<col align=\"right\">\n<\/colgroup>\n<tbody>\n<tr>\n<td align=\"center\" rowspan=\"3\">Computer A<\/td>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = int(b \/ c)<\/td>\n<td align=\"center\">+ 12%<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.1957<\/td>\n<td align=\"right\">0.0161<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = b \/ c : integers<\/td>\n<td align=\"center\">+ 23%<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.2153<\/td>\n<td align=\"right\">0.0096<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = b \/ c : Numbers<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.1752<\/td>\n<td align=\"right\">0.0064<\/td>\n<\/tr>\n<\/tbody>\n<colgroup span=\"0\" style=\"color:#000000;\"><\/colgroup>\n<tbody>\n<tr>\n<td align=\"center\" rowspan=\"3\">Computer B<\/td>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = int(b \/ c)<\/td>\n<td align=\"center\">+ 285%<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.3005<\/td>\n<td align=\"right\">0.0156<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = b \/ c : integers<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.078<\/td>\n<td align=\"right\">0<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = b \/ c : Numbers<\/td>\n<td align=\"center\">+ 8%<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.0843<\/td>\n<td align=\"right\">0.0158<\/td>\n<\/tr>\n<\/tbody>\n<colgroup style=\"color:#000000;\"><\/colgroup>\n<tbody>\n<tr>\n<td align=\"center\" rowspan=\"3\">Computer C<\/td>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = int(b \/ c)<\/td>\n<td align=\"center\">+ 234%<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.314<\/td>\n<td align=\"right\">0.0037<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = b \/ c : integers<\/td>\n<td align=\"center\">+ 334%<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.4081<\/td>\n<td align=\"right\">0.0409<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = b \/ c : Numbers<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.094<\/td>\n<td align=\"right\">0<\/td>\n<\/tr>\n<\/tbody>\n<colgroup style=\"color:#000000;\"><\/colgroup>\n<tbody>\n<tr>\n<td align=\"center\" rowspan=\"3\">Computer D<\/td>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = int(b \/ c)<\/td>\n<td align=\"center\">+ 225%<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.103<\/td>\n<td align=\"right\">0.0073<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = b \/ c : integers<\/td>\n<td align=\"center\">+ 385%<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.154<\/td>\n<td align=\"right\">0.0055<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = b \/ c : Numbers<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.0317<\/td>\n<td align=\"right\">0.0005<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table cellspacing=\"0\" cellpadding=\"4\" width=\"470\" rules=\"groups\">\n<tbody>\n<tr>\n<th width=\"80\" align=\"left\">&nbsp;<\/th>\n<th width=\"140\" align=\"left\" bgcolor=\"#D8D8D8\">Multiplication<\/th>\n<th width=\"120\" align=\"center\">Relative Speed<\/th>\n<th width=\"60\" align=\"right\" bgcolor=\"#D8D8D8\">Average<\/th>\n<th width=\"70\" align=\"right\">Std. Dev.<\/th>\n<\/tr>\n<\/tbody>\n<colgroup style=\"color:#000000;\"><\/colgroup>\n<colgroup>\n<col align=\"center\">\n<col align=\"left\">\n<col align=\"center\">\n<col align=\"right\">\n<col align=\"right\">\n<\/colgroup>\n<tbody>\n<tr>\n<td align=\"center\" rowspan=\"3\">Computer A<\/td>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = int(b * c)<\/td>\n<td align=\"center\">+ 275%<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.1477<\/td>\n<td align=\"right\">0.0198<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = b * c : integers<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.0395<\/td>\n<td align=\"right\">0.008<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = b * c : Numbers<\/td>\n<td align=\"center\">+ 12%<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.0441<\/td>\n<td align=\"right\">0.0151<\/td>\n<\/tr>\n<\/tbody>\n<colgroup style=\"color:#000000;\"><\/colgroup>\n<tbody>\n<tr>\n<td align=\"center\" rowspan=\"3\">Computer B<\/td>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = int(b * c)<\/td>\n<td align=\"center\">+ 460%<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.1748<\/td>\n<td align=\"right\">0.0118<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = b * c : integers<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.0312<\/td>\n<td align=\"right\">0.0004<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = b * c : Numbers<\/td>\n<td align=\"center\">+ 126%<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.0831<\/td>\n<td align=\"right\">0.0156<\/td>\n<\/tr>\n<\/tbody>\n<colgroup style=\"color:#000000;\"><\/colgroup>\n<tbody>\n<tr>\n<td align=\"center\" rowspan=\"3\">Computer C<\/td>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = int(b * c)<\/td>\n<td align=\"center\">+ 210%<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.2915<\/td>\n<td align=\"right\">0.0159<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = b * c : integers<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.0936<\/td>\n<td align=\"right\">0.0005<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = b * c : Numbers<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.094<\/td>\n<td align=\"right\">0<\/td>\n<\/tr>\n<\/tbody>\n<colgroup style=\"color:#000000;\"><\/colgroup>\n<tbody>\n<tr>\n<td align=\"center\" rowspan=\"3\">Computer D<\/td>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = int(b * c)<\/td>\n<td align=\"center\">+ 146%<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.0780<\/td>\n<td align=\"right\">0<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = b * c : integers<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.269<\/td>\n<td align=\"right\">0.0090<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = b * c : Numbers<\/td>\n<td align=\"center\">+ 18%<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.0317<\/td>\n<td align=\"right\">0.0004<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table cellspacing=\"0\" cellpadding=\"4\" width=\"470\" rules=\"groups\">\n<tbody>\n<tr>\n<th width=\"80\" align=\"left\">&nbsp;<\/th>\n<th width=\"140\" align=\"left\" bgcolor=\"#D8D8D8\">Bit Shifting<\/th>\n<th width=\"120\" align=\"center\">Relative Speed<\/th>\n<th width=\"60\" align=\"right\" bgcolor=\"#D8D8D8\">Average<\/th>\n<th width=\"70\" align=\"right\">Std. Dev.<\/th>\n<\/tr>\n<\/tbody>\n<colgroup span=\"0\" style=\"color:#000000;\"><\/colgroup>\n<colgroup>\n<col align=\"center\">\n<col align=\"left\">\n<col align=\"center\">\n<col align=\"right\">\n<col align=\"right\">\n<\/colgroup>\n<tbody>\n<tr>\n<td align=\"center\" rowspan=\"3\">Computer A<\/td>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = int(b) &lt;&lt; int(c)<\/td>\n<td align=\"center\">+ 645%<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.2937<\/td>\n<td align=\"right\">0.0308<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = b &lt;&lt; c : integers<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.0395<\/td>\n<td align=\"right\">0.008<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">a = b &lt;&lt; c : Numbers<\/td>\n<td align=\"center\">+ 1,860%<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.6954<\/td>\n<td align=\"right\">0.0097<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Flash holds to the basic speed hierarchy that bit shifting &lt; addition &lt; multiplication &lt; division; and bit shifting, addition, and multiplication are much closer in speed than I anticipated.  Bit shifting expects integers, so its poor results with Number types is expected, these results were consistant across all computers.  Computer A performed the worst when using number types for bit shifting; however, even with casting all computers were atleast 5 times (500%) slower with numbers.  While it is not as fast as using Number types to begin with, this is also a prime example of how casting can save you some processor time.  There is a benchmark example working with arrays that proves casting an object to its current type has essentially no cost.<\/p>\n<p>Addition promotion concerns and Iterator speed, 10,000,000 computations per trial. Run the benchmarks: <a href=\"http:\/\/stephencalenderblog.com\/becnhmarks\/BenchmarkAddition.php\">adding<\/a>, <a href=\"http:\/\/stephencalenderblog.com\/becnhmarks\/BenchmarkIterator.php\">iterating<\/a><\/p>\n<table cellspacing=\"0\" cellpadding=\"4\" width=\"470\" rules=\"groups\">\n<tbody>\n<tr>\n<th width=\"80\" align=\"left\">&nbsp;<\/th>\n<th width=\"140\" align=\"left\" bgcolor=\"#D8D8D8\">Addition Promotion<\/th>\n<th width=\"120\" align=\"center\">Relative Speed<\/th>\n<th width=\"60\" align=\"right\" bgcolor=\"#D8D8D8\">Average<\/th>\n<th width=\"70\" align=\"right\">Std. Dev.<\/th>\n<\/tr>\n<\/tbody>\n<colgroup span=\"0\" style=\"color:#000000;\"><\/colgroup>\n<colgroup>\n<col align=\"center\">\n<col align=\"left\">\n<col align=\"center\">\n<col align=\"right\">\n<col align=\"right\">\n<\/colgroup>\n<tbody>\n<tr>\n<td align=\"center\" rowspan=\"3\">Computer A<\/td>\n<td align=\"left\" bgcolor=\"#D8D8D8\">All Numbers<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.0346<\/td>\n<td align=\"right\">0.0062<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">All Integers<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.0358<\/td>\n<td align=\"right\">0.0068<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">Mixed<\/td>\n<td align=\"center\">+ 227%<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.1146<\/td>\n<td align=\"right\">0.0074<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table cellspacing=\"0\" cellpadding=\"4\" width=\"470\" rules=\"groups\">\n<tbody>\n<tr>\n<th width=\"80\" align=\"left\">&nbsp;<\/th>\n<th width=\"140\" align=\"left\" bgcolor=\"#D8D8D8\">Iterator<\/th>\n<th width=\"120\" align=\"center\">Relative Speed<\/th>\n<th width=\"60\" align=\"right\" bgcolor=\"#D8D8D8\">Average<\/th>\n<th width=\"70\" align=\"right\">Std. Dev.<\/th>\n<\/tr>\n<\/tbody>\n<colgroup span=\"0\" style=\"color:#000000;\"><\/colgroup>\n<colgroup>\n<col align=\"center\">\n<col align=\"left\">\n<col align=\"center\">\n<col align=\"right\">\n<col align=\"right\">\n<\/colgroup>\n<tbody>\n<tr>\n<td align=\"center\" rowspan=\"3\">Computer A<\/td>\n<td align=\"left\" bgcolor=\"#D8D8D8\">Unsigned Integer<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.0331<\/td>\n<td align=\"right\">0.0054<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">Integer<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.0321<\/td>\n<td align=\"right\">0.0040<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">Number<\/td>\n<td align=\"center\">+ 260%<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.1154<\/td>\n<td align=\"right\">0.0078<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Flash does this thing called \u2018promotion\u2019 where if it is unsure of what type to return it will default to the more complex type, this happends when you mix types in opperations, which must include the ++ operator.  When Flash is unsure of the type it takes it a while to figure out what it should cast the object to, which is why casting is so important.  Results were comparable across all computers, on mixed type addition the percentage increase in time ranged from +200% to +500%, number type iterating ranged from +20% to +260%.<\/p>\n<p>Comparing Unary, Postfix, and Verbose statements, 10,000,000 computations per trial. Run the benchmark: <a href=\"http:\/\/stephencalenderblog.com\/becnhmarks\/BenchmarkPostfix.php\">All the ways you can add by 1<\/a><\/p>\n<table cellspacing=\"0\" cellpadding=\"4\" width=\"470\" rules=\"groups\">\n<tbody>\n<tr>\n<th width=\"80\" align=\"left\">&nbsp;<\/th>\n<th width=\"140\" align=\"left\" bgcolor=\"#D8D8D8\">Operation<\/th>\n<th width=\"120\" align=\"center\">Relative Speed<\/th>\n<th width=\"60\" align=\"right\" bgcolor=\"#D8D8D8\">Average<\/th>\n<th width=\"70\" align=\"right\">Std. Dev.<\/th>\n<\/tr>\n<\/tbody>\n<colgroup span=\"0\" style=\"color:#000000;\"><\/colgroup>\n<colgroup>\n<col align=\"center\">\n<col align=\"left\">\n<col align=\"center\">\n<col align=\"right\">\n<col align=\"right\">\n<\/colgroup>\n<tbody>\n<tr>\n<td align=\"center\" rowspan=\"3\">Computer A<\/td>\n<td align=\"left\" bgcolor=\"#D8D8D8\">++x<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.0363<\/td>\n<td align=\"right\">0.0075<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">x++<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.0374<\/td>\n<td align=\"right\">0.0078<\/td>\n<\/tr>\n<tr>\n<td align=\"left\" bgcolor=\"#D8D8D8\">x = x + 1<\/td>\n<td align=\"center\">&#8212;<\/td>\n<td align=\"right\" bgcolor=\"#D8D8D8\">0.0427<\/td>\n<td align=\"right\">0.0071<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>I had a professor that claimed ++x was faster than x++, and he appears to be correct, but not enough to significantly matter.<\/p>\n<p>Lessons Learned:<\/p>\n<ul>\n<li> Never divide unless you absolutely need to.<\/li>\n<li> If you are unsure about the type an operation returns, cast.<\/li>\n<li> Stick to integer types and postfix for your iterators.<\/li>\n<li> Always type your variables, besides being easier to read and debug, it is much faster.<\/li>\n<\/ul>\n<p>Be sensible, all of these operations are in the 10,000,000 range and are among the fastest operations any language can perform.  Addition, multiplication, and bit shifting would probably need 100,000,000 or 1,000,000,000 computations per trial before we would see a sizable speed difference; relatively they are so fast that they just are not worth worrying about.  Choosing the right data types and operations will speed up you programs but they live in the subset of the fastest code.  Unless you are doing an incredible volume of mathematical operations, your desired target of the slowest running code is something else.<\/p>\n<p><a href=\"https:\/\/www.stephencalenderblog.com\/?p=19\">Continue to Data Structure Speed<\/a><\/p>\n<p>This post is part of a series:<br \/>\n<a href=\"https:\/\/www.stephencalenderblog.com\/?p=7\">Introduction<\/a><br \/>\n<a href=\"https:\/\/www.stephencalenderblog.com\/?p=9\">Methodology<\/a><br \/>\n<a href=\"https:\/\/www.stephencalenderblog.com\/?p=12\">Operation Speed<\/a> &lt;&#8211; You are here<br \/>\n<a href=\"https:\/\/www.stephencalenderblog.com\/?p=19\">Data Structure Speed<\/a><br \/>\n<a href=\"https:\/\/www.stephencalenderblog.com\/?p=22\">Graphics Speed<\/a><br \/>\n<a href=\"https:\/\/www.stephencalenderblog.com\/?p=25\">Results<\/a><\/p>\n<table cellspacing=\"15\">\n<tbody>\n<tr>\n<td>\n<a class=\"DiggThisButton DiggMedium\"><\/a><\/td>\n<td>\n<a href=\"http:\/\/reddit.com\/submit\" onclick=\"window.location = 'http:\/\/reddit.com\/submit?url=' + encodeURIComponent(window.location); return false\"> <img decoding=\"async\" src=\"http:\/\/reddit.com\/static\/spreddit7.gif\" alt=\"submit to reddit\" border=\"0\"> <\/a><\/td>\n<td>\n<table>\n<tbody>\n<tr>\n<td>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/static.delicious.com\/img\/delicious.small.gif\" height=\"10\" width=\"10\" alt=\"Delicious\"><\/td>\n<td>\n<a href=\"http:\/\/delicious.com\/save\" onclick=\"window.open('http:\/\/delicious.com\/save?v=5&amp;noui&amp;jump=close&amp;url='+encodeURIComponent(location.href)+'&amp;title='+encodeURIComponent(document.title), 'delicious','toolbar=no,width=550,height=550'); return false;\"> Bookmark this on Delicious<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/td>\n<td>\n<script src=\"http:\/\/www.stumbleupon.com\/hostedbadge.php?s=2\"><\/script><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>All of these trials compare basic operation speed, alternative syntax, and mathematical substitutions.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"class_list":["post-12","post","type-post","status-publish","format-standard","hentry","category-actionscript-30","category-optimization"],"_links":{"self":[{"href":"https:\/\/www.stephencalenderblog.com\/index.php?rest_route=\/wp\/v2\/posts\/12","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.stephencalenderblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stephencalenderblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stephencalenderblog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stephencalenderblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12"}],"version-history":[{"count":29,"href":"https:\/\/www.stephencalenderblog.com\/index.php?rest_route=\/wp\/v2\/posts\/12\/revisions"}],"predecessor-version":[{"id":833,"href":"https:\/\/www.stephencalenderblog.com\/index.php?rest_route=\/wp\/v2\/posts\/12\/revisions\/833"}],"wp:attachment":[{"href":"https:\/\/www.stephencalenderblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stephencalenderblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stephencalenderblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}