-1

My question is very simple. I have a python simple code like this :

for i in range(1,1193616,1) :
        print i

So all number in range 1 until 1193616 will be printed and this looping progress took a very long time.. How to make it fast ?

EDIT :

actually, i try to make an A-star Pathfinding program for image data (Raster).. This are my A-star function script so far :

def A_star(h,c,dx,dy,u,s_id,e_id,Op,Cl,Prt,CC,o,ht,w):
    Op.append(s_id) 
    while e_id not in Op : 
        if Op == [ ] :
            break
        candidate = { }
        for i in Op :
            d = {i : CC[i]}
            candidate.update(d)
        o = min(candidate, key=candidate.get)
        Cl.append(o)
        Op.remove(o)
        adjacent_list = adjacent_cell(o,dx,dy )
        for p in adjacent_list :
            if p in Cl:       
                adjacent_list = filter(lambda i: i != p, adjacent_list)    
            elif p not in Op :   
                Op.append(p)
                d = {p : o }
                Prt.update(d)
                d = {p : F(p,o,h,u,w,c,dx,e_id,ht,CC)}
                CC.update(d)
            elif id in Op : 
                f1 = F(p,o,h,u,w,c,dx,e_id,ht,CC) 
                f2 = F(p,Prt[p],h,u,w,c,dx,e_id,ht,CC)
                if f1 < f2 :  
                    d = {p : o }
                    Prt.update(d)
                    d = {id : F(p,o,h,u,w,c,dx,e_id,ht,CC)}
                    CC.update(d)
    return Prt

assumed that s_id = 1 (start) and e_id = 1193616 (end), long time looping progress happened on line 3 while e_id not in Op : Is there any way to accelerate or optimization my code ?

Faizalprbw
  • 529
  • 1
  • 6
  • 17
  • 5
    I'm pretty sure the printing is your bottle neck. Try only putting `pass` as the loop body and print once the loop is finished. Should be a lot faster. – k-nut Jul 28 '15 at 12:22
  • possible duplicate of [Why is printing to stdout so slow? Can it be sped up?](http://stackoverflow.com/questions/3857052/why-is-printing-to-stdout-so-slow-can-it-be-sped-up) – Stiffo Jul 28 '15 at 12:23
  • It's not the looping, it's writing unbuffered text to the console. – Stiffo Jul 28 '15 at 12:23
  • Is it printing what you want to do? Why 1193616? Printing is slow. If you want to do something else than printing we need more details about it to know if you can actually optimize it or not. – cnluzon Jul 28 '15 at 12:24
  • As already said few times, the loop is fine, but in case of Python 2 you may prefer `xrange` for performance. – bereal Jul 28 '15 at 12:25
  • You should probably ask how to optimize your A-star pathfinding implementation as a separate question. – jfs Jul 28 '15 at 20:34
  • I agree and thanks for your suggestion, @J.F.Sebastian, i've been re-ask my question.. – Faizalprbw Jul 28 '15 at 21:49

5 Answers5

6

Usually what is slow is the output, not the loop; you could do:

print '\n'.join('%s' % c for c in range(1, 1193616))

Edit:

On my system, when the output is a terminal, your code took 10.193s, and my version 2.374s. If it's redirected to a file, it's 0.486s vs 0.572s (not much of a difference).

Note it is not always the best solution to "buffer" all the data into your program before writing it in one go (you could exhaust your memory resources), and I doubt it is useful to print to a terminal more than a hundreds lines...

bufh
  • 3,153
  • 31
  • 36
1

The loop itself is quite efficient, it's actually the "print" statement that causes the overhead. Replace the print with a variable assignment, for instance, and you'll see the difference. Also, it's worth mentioning that your "range" call can be simplified to range(1193616)

mephisto
  • 661
  • 1
  • 6
  • 14
1

Instead of printing the numbers in each iteration join them and print once,but since str.join accept string objects you can convert them to string by applying the str function on its number using map function :

print '\n'.join((map(str,range(1,1193616))))
Mazdak
  • 105,000
  • 18
  • 159
  • 188
1

With the problem defined that way there's not much you can do. Most optimization involves reducing the amount of work performed by the processor. This can mean adjusting the data so that there is less to be processed.

For example, if you reduced the number of iterations by half, it will be faster! If that can't be changed, then you can't do much about it.

Otherwise, change the operations that are performed. In this case change the 'print' statement to 'pass'. Of course, the result won't be the same.

If you are using Python 2, then range() actually allocates a list, allocates the number objects and populates the list before your loop even starts. Python 3 returns an iterator that avoids all the memory allocation and initialization and so is faster. (Python 2 has an xrange() function that works like that, but you are better off using Python 3 anyway)

Usually the code being optimized is much more complex. It may involve calling functions, operating on lists, strings, or other data structures. The solutions, then, are to understand the intended result and how to achieve that result with less work. Compare it to making a trip to the store. You could drive all around the city, stop at every light you can and pass through every neighborhood, or you could go directly around the block to the nearest store. One is "optimized" even though both "work".

dsh
  • 12,037
  • 3
  • 33
  • 51
0

As was mentioned above, Xrange should be a bit faster, e.g. for i in xrange(n): do something. What's your program supposed to be doing?