I am working on a code with relatively few iterations (like, less than 100) and the total time is still several minutes. This means complexity is of no help and I don't really know what operations take most of the cpu time. This is annoying as i want to repeat that operation thousands of times. I want to reduce that time.  I know how to use time.clock() and return the difference but i am looking for a way to know the time spent on each line to know where and if an improvement can be made. Is there a mode already built for that on python? (3.4, windows)
19675
and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With Python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to __main__.  What is a good way to profile how long a Python program takes to run?
26439
Is there any performance difference between the for loops on a primitive array?   Assume:  double[] doubleArray = new double[300000];   for (double var: doubleArray)     someComplexCalculation(var);   or :  for ( int i = 0, y = doubleArray.length; i &lt; y; i++)    someComplexCalculation(doubleArray[i]);   Test result  I actually profiled it:  Total timeused for modern loop= 13269ms Total timeused for old loop   = 15370ms   So the modern loop actually runs faster, at least on my Mac OSX JVM 1.5.
56244
I have a method that I would like to call.  However, I'm looking for a clean, simple way to kill it or force it to return if it is taking too long to execute.  I'm using Java.  to illustrate:  logger.info("sequentially executing all batches..."); for (TestExecutor executor : builder.getExecutors()) { logger.info("executing batch..."); executor.execute(); }   I figure the TestExecutor class should implement Callable and continue in that direction.  But all i want to be able to do is stop executor.execute() if it's taking too long.  Suggestions...?  EDIT  Many of the suggestions received assume that the method being executed that takes a long time contains some kind of loop and that a variable could periodically be checked. However, this is not the case.  So something that won't necessarily be clean and that will just stop the execution whereever it is is acceptable.
82458
In order to optimize my script, I would need some help to perform a rotation around the cursor (see )  With low-level python, that means, without bpy.ops.* operations.  I have to admit that I didn't find a good overview on how to make rotation with python without bpy.ops. Do I have to perform matrix operations for a simple rotation around an axis Z located at the cursor position?
65208
Is there a common way to get the current time in or with milliseconds?  There is os.time(), but it only provides full seconds.
73553
What is meant by "Constant Amortized Time" when talking about time complexity of an algorithm?
34594
I would like to limit the amount of CPU time dedicated to certain processes (e.g. Firefox, Safari, ...). I can't understand why such programs, even when not used (nothing loading, no animations, running in the background, ...), are so resource hungry. Why must a browser eat 50% or more of my CPU? Can I limit it to 10%?
13652
time is a brilliant command if you want to figure out how much CPU time a given command takes.  I am looking for something similar that can measure the max RAM usage of the program and any children. Preferably it should distinguish between allocated memory that was used and unused. Maybe it could even give the median memory usage (so the memory usage you should expect when running for a long time).  So I would like to do:  rammeassure my_program my_args   and get output similar to:  Max memory allocated: 10233303 Bytes Max memory used: 7233303 Bytes Median memory allocation: 5233303 Bytes   I have looked at memusg  but I regard that as somewhat a hack.
6362
I need to calculate the time complexity of the following code:  for (i = 1; i &lt;= n; i++) {   for(j = 1; j &lt;= i; j++)   {    // Some code   } }   Is it O(n^2)?
37679
I'm calling a function in Python which I know may stall and force me to restart the script.   How do I call the function or what do I wrap it in so that if it takes longer than 5 seconds the script cancels it and does something else?
107478
If I time a process using the time command, I get output for 'real', 'user', and 'sys'.  My understanding from  is that 'real' is wall time, whereas 'user' and 'sys' are process time.  Does this imply that 'user' and 'sys' will be unaffected by other processes? In other words, if the computer is under heavy load or light load from other processes, it may take more time on the wall clock ('real') to finish my process. But my process may have only required 5 seconds of running time, even though it was spread out over 20 seconds of real-world time.  Am I guaranteed that I'll be told '5 seconds user time' regardless of whatever else the system is doing?
99535
I'm making an python script which create a lot of object and this could take a lot of time. My question is how to show to the user of my script, the "progression" of the script? For now I print an progress bar in the system console.  But you have to open the system console befor and (apparently) the system console only work with Windows.  Is there an other way to show the progression of my script with python like control the mouse percentage icon (the little icon that replace the mouse while baking) or another idea?
5727
I want to do some benchmarking of a C# process, but I don't want to use time as my vector - I want to count the number of IL instructions that get executed in a particular method call. Is this possible?  Edit I don't mean static analysis of a method body - I'm referring to the actual number of instructions that are executed - so if, for example, the method body includes a loop, the count would be increased by however many instructions make up the loop * the number of times the loop is iterated.
45500
I developing a simple real-time strategy game in a small team with no experience, and we're considering using one thread per game object (tank, soldier, etc). Is this a good idea or is it overkill?  The thread would call each object on each instant change (game timer, not graphic engine timer)  We're using Python with the Panda3D engine, meaning the GIL is probably going to reduce a lot the possible speed improvements.
75002
In my script, I have for loop over many cube objects (~1000) and the treatment is very slow. Looking more in details, I notice that in the same amount of loops:   if I use python operation or simple blender operation like  obj = bpy.data.objects[obj_name]   or   obj.select = True   it takes less than 0.08s (for all 1000 objects) but as soon as I start using blender operators like:  bpy.ops.object.select_pattern() # or bpy.ops.object.duplicate() # or bpy.ops.object.location_clear() # or bpy.ops.object.transform_apply() # etc.   then, the performance drops down deeply with more than 6s for the same amount of objects.   And, a last information, if I reduce the amount of objects from 1000 to 50, the same set of operations with bpy.ops is 0.03s - so extrapolating, it would be 0.6s for the 1000 objects and not 6s. It is as is with 1000 objects, we loose a factor 10 in speed in comparison than with 50 objects.  I tried reducing the complexity of my mesh changing the cubes in planes but it had no effect at whole on performance.  Is there something particular to know to improve these performance or a way to use the bpy.ops methods with better performances? Obviously, I am missing something important probably.
112148
I'm reading Malcolm Clark's A Plain TeX Primer. On page 111 there's an exercise to define a macro that prints the current time. I've reproduced Clark's answer below:  \newcount\minleft \newcount\timehour \def\thetime{\timehour=\time   \divide\timehour by60 %gives 24 hour part of clock   \minleft=\timehour   \multiply\minleft by-60 %   \advance\minleft by\time    %minutes after hour   \ifnum\time&gt;720\advance\timehour by-12\fi\relax   \number\timehour:\ifnum\minleft&lt;10 %                    0\fi\relax\number\minleft   \ifnum\time&gt;720~p.m.\else~a.m.\fi}   My problem is that I don't know what \relax is doing in lines 8 and 10 (after each \fi). When I use this macro, it prints the time as it's supposed to:  8:51 p.m.  If I delete the \relax in line 8, I get the time but with the hour missing:  :51 p.m.  If I delete the \relax in line 10 (and restore the \relax in line 8), the macro works again like it did the first time:  8:51 p.m.  I naively thought that the \fi that ends the \ifnum block ends the processing of whatever's going on in the \ifnum ... \fi, and that it wouldn't matter whether \relax is there or not, in either place. Clearly that's not the case, but I can't figure out what's happening in line 8, and also why the \relax doesn't seem to be necessary in line 10.  In a different context the author does explain that \relax is a "do nothing" command that (in this other context) prevents TeX from treating the expression before the \relax and after the \relax parts of the same expression. I get that. But concerning \relax after an \fi, he says:  "The \relax is probably not required, but it's always nice to have one in: perhaps an example of 'safe TeX'."  This might be good advice, but it is not enlightening.  I found the following questions about \relax on tex.se, but I don't think they answer my question (or at least I'm not smart enough to figure it out):          So, what is \relax actually doing in this macro?
78277
For example, files, in Python, are iterable - they iterate over the lines in the file. I want to count the number of lines.   One quick way is to do this:  lines = len(list(open(fname)))   However, this loads the whole file into memory (at once). This rather defeats the purpose of an iterator (which only needs to keep the current line in memory).  This doesn't work:  lines = len(line for line in open(fname))   as generators don't have a length.  Is there any way to do this short of defining a count function?  def count(i):     c = 0     for el in i: c += 1     return c   To clarify, I understand that the whole file will have to be read! I just don't want it in memory all at once