Write a program or function, which doesn't take any input, and outputs all the integer numbers, between -1000 and 1000 in ascending order, to the stdout, one per line, like this:


And after that you need to print the time taken to print these numbers, or the time from the start of the program's execution in milliseconds (if necessary, it can also contain some other things, for example: time taken:xxxms is ok). It can be a float, or an integer (if you print an integer, you need to round down to the nearest).

Example code

using System;
using System.Diagnostics;
class P
    static void Main(string[] args)
        Stopwatch st = Stopwatch.StartNew();
        for (int i = -1000; i <= 1000; i++)


Standard loopholes are not allowed

Other infos

It's code golf, so the shortest submission wins.

  • \$\begingroup\$ @GurupadMamadapur No, sorry \$\endgroup\$ – Horváth Dávid Jan 14 '17 at 18:15
  • \$\begingroup\$ Why? I think essentially to print those numbers every statement is involved from the start of the program right? \$\endgroup\$ – Gurupad Mamadapur Jan 14 '17 at 18:19
  • 1
    \$\begingroup\$ @GurupadMamadapur Ok, you're right, I will edit the question accordingly. \$\endgroup\$ – Horváth Dávid Jan 14 '17 at 18:32
  • \$\begingroup\$ Can the program wait some amount of time from the start, and print that amount? \$\endgroup\$ – xnor Jan 15 '17 at 1:54
  • \$\begingroup\$ @xnor I think, that would change the challenge, and because there are already lots of answers to the original challenge, I would say no. \$\endgroup\$ – Horváth Dávid Jan 15 '17 at 10:29

38 Answers 38


MATL, 13 bytes


Try it online!

       % Implicitly start timer
1e3    % Push 1000
       % STACK: 1000
t_     % Duplicate, negate
       % STACK: 1000, -1000
y      % Duplicate second-top number
       % STACK: 1000, -1000, 1000
&:     % Two-input range
       % STACK: 1000, [-1000, 999, ..., 1000]
!      % Transpose into column vector
       % STACK: 1000, [-1000; 999; ...; 1000]
D      % Display
       % STACK: 1000
Z`     % Push timer value, say t
       % STACK: 1000, t
*      % Multiply
       % STACK: 1000*t
       % Implicitly display
  • 2
    \$\begingroup\$ Very nice! Smart to implement: Implicitly start timer. Was that there from day one, or is that the result of an earlier challenge? \$\endgroup\$ – Stewie Griffin Jan 14 '17 at 20:30
  • \$\begingroup\$ @StewieGriffin Not from day one. I added it on 13 Jul 2016, probably after having to intiallize it explicitly in a couple of challenges \$\endgroup\$ – Luis Mendo Jan 15 '17 at 2:05

Octave, 46 43 36 30 23 bytes


This will print:

ans =


If you don't like the ans = , then we have to add an additional 6 bytes for disp:


Saved a lot of bytes thanks to a few reminders from rahnema1.


tic;                              % Starts timer
         (-1e3:1e3)'              % A vertical vector -1000 ... 1000
    disp((-1e3:1e3)'),            % Display this vector
                      toc*1e3     % Stop the timer and output the time in milliseconds

JavaScript, 60 bytes


In order to get all the events logged, you should use the script from the developer console (otherwise the logs are erased after the certain amount of them).

  • \$\begingroup\$ i=~1e3 to save a byte :-) \$\endgroup\$ – ETHproductions Jan 14 '17 at 18:29

CJam, 18 bytes


Try it online!

How it works

es                  Push the current time (milliseconds since epoch) on the stack.
  2001{     }/      For each integer X from 0 to 2000:
       1e3-           Subtract 1000 from X.
           n          Print with a newline.
              es    Push the current time on the stack.
                \-  Swap and subtract.

Python 3.5, 80 77 73 bytes

import time

Previous solutions involved using timeit and time.time(), they were larger.

Sadly, time.process_time() was introduced in python 3.3.

Thanks to Dennis for saving 4 bytes!


Bash (+coreutils), 41, 49, 46, 44, 42 bytes


  • Refactored to use Bash-builtin (time), to address @Dennis precision concerns;
  • Reduced by 3 bytes, by utilizing Bash 4+ |& for stderr redirection;
  • Saved 2 more bytes by replacing seq -1000 1000 with seq -1e3 1e3 (Thanks @Dennis !);
  • -2 bytes by removing unnecessary backslash, and using default precision (Thx @Dennis !).


TIMEFORMAT=%R*1000;(time seq -1e3 1e3)|&bc

Try It Online !


Using a coreutils "time" utility, instead of the Bash-builtin, results in a 41, 35 byte solution:

\time -f "%e*1000" seq -1e3 1e3|&bc

"\" is here to make bash invoke the real command, instead of the builtin.

Unfortunately coreutils time precision is only 1/100s, which have raised concerns on if it is a valid solution.


R, 42 bytes


This will print


To remove elapsed, two additional bytes are necessary:


Bash + GNU utils, 43

  • Saved 2 bytes thanks to @Dennis
  • Saved 5 bytes thanks to @zeppelin
c=date\ +%s%3N
seq -1e3 1e3

The date command gives the number of seconds since the epoch concatenated with current nanoseconds. This command is run before and after. bc takes the difference and prints.

Try it online.

I was hoping to do this for 17:

time seq -1e3 1e3

But the output of time gives more than we need:

real    0m0.004s
user    0m0.000s
sys 0m0.004s
  • 1
    \$\begingroup\$ You can save two bytes by replacing 1000 with 1e3. \$\endgroup\$ – Dennis Jan 14 '17 at 18:27
  • \$\begingroup\$ "But the output of time gives ...." ...perhaps you should man bash. \$\endgroup\$ – H Walters Jan 14 '17 at 19:08
  • 1
    \$\begingroup\$ You can probably save a few bytes, by capturing the date with ms precision directly, like this: date +%s%3N. \$\endgroup\$ – zeppelin Jan 14 '17 at 22:14

JavaScript (ES6), 63 59 bytes


  • \$\begingroup\$ Nice. You can save three bytes by removing the space in new (d=Date) and starting at -1000: for(t=new(d=Date),c=console.log,i=~1e3;i<1e3;c(++i));c(new d-t) \$\endgroup\$ – ETHproductions Jan 14 '17 at 18:28
  • \$\begingroup\$ @ETHproductions thanks :) the ~1e3 is a great touch. \$\endgroup\$ – George Reith Jan 14 '17 at 18:33
  • 1
    \$\begingroup\$ In the snippet output is from only 952 to 1000 why is that? \$\endgroup\$ – Gurupad Mamadapur Jan 14 '17 at 18:54
  • \$\begingroup\$ @GurupadMamadapur The snippet output is limited to 50 lines. (Or more precisely: the 50 last lines.) \$\endgroup\$ – Arnauld Jan 14 '17 at 20:10
  • 1
    \$\begingroup\$ @IsmaelMiguel Amazing wasn't aware of performance.now() or the Performance interface at all \$\endgroup\$ – George Reith Jan 16 '17 at 20:01

R, 66 bytes

x=proc.time();for(i in -1e3:1e3)cat(i,"\n");(proc.time()-x)[3]*1e3

Probably not the shortest but it works.

  • \$\begingroup\$ Can proc.time be stored in a variable? t=proc.time;x=t(); ... \$\endgroup\$ – AnnanFay Dec 31 '17 at 0:34

Mathematica, 51 bytes




Store the Print function in p. Print 2001 numbers, starting at -1000, incrementing by 1.

AbsoluteTiming@ ...

Find the total time elapsed in seconds.

p[1*^3#]&@@ ...

Multiply that by 1000 (seconds -> miliseconds) and p (Print) it.

  • \$\begingroup\$ Argh, you beat me by 3 minutes! :) Are you sure Timing doesn't satisfy the (slightly vague) problem description as well as AbsoluteTiming? \$\endgroup\$ – Greg Martin Jan 14 '17 at 20:26
  • 2
    \$\begingroup\$ @GregMartin Timing outputs the CPU time and does not include the time taken by the front end. That is. the time taken to increment the counter in Array is counted, but the time taken to display those numbers on screen is not counted. This effect can be seen in this simple example: Timing@Print@3 gives 0 seconds, but AbsoluteTiming@Print@3 does not. \$\endgroup\$ – JungHwan Min Jan 14 '17 at 21:28

PHP, 110 70 bytes

still a little long; but saved 38 with @AlexHowansky´s hint, and two more with 1e3 and ~1e3.


prints float. Run with -r.

  • 2
    \$\begingroup\$ You can pass microtime() a truthy value and it will return a float directly, you don't have to add the strings. \$\endgroup\$ – Alex Howansky Jan 14 '17 at 21:18
  • \$\begingroup\$ -30% with that hint. I wish I could do ten upvotes on your comment. :D \$\endgroup\$ – Titus Jan 15 '17 at 15:09
  • \$\begingroup\$ It's sad how PHP doesn't have something that returns the time in milliseconds. Like Javascript has. I can't suggest improvements. It's as small as it can get. Well done! \$\endgroup\$ – Ismael Miguel Jan 16 '17 at 15:24
  • \$\begingroup\$ @IsmaelMiguel I think JavaScript doesn´t have microseconds. :) \$\endgroup\$ – Titus Jan 16 '17 at 15:49
  • \$\begingroup\$ @Titus What I meant is that Javascript's dates are all handled in milliseconds, while PHP only had seconds or microseconds. \$\endgroup\$ – Ismael Miguel Jan 16 '17 at 16:05

Powershell, 27 Bytes


Thanks to AdmBorkBork for pointing out that the verbose default output is acceptable in the challenge.

Outputs result like:


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 5
Milliseconds      : 679
Ticks             : 56799255
TotalDays         : 6.57398784722222E-05
TotalHours        : 0.00157775708333333
TotalMinutes      : 0.094665425
TotalSeconds      : 5.6799255
TotalMilliseconds : 5679.9255

for more contained result of just milliseconds, use original answer:


Save time before as $1, print to stdout automatically, then get the time between the beginning and end of execution.

  • \$\begingroup\$ You could just pipe the range to oh (Out-Host) which will bypass the fact that Measure-Command captures the pipeline. Example at TIO \$\endgroup\$ – AdmBorkBork Jan 19 '17 at 17:14
  • \$\begingroup\$ @AdmBorkBork the point was that I don't think it will save bytes, unless I'm missing something. \$\endgroup\$ – colsw Jan 19 '17 at 17:23
  • \$\begingroup\$ Measure-Command{-1e3..1e3|oh} is 29 bytes. Sure, it prints out extra stuff thanks to the Measure-Command, but the challenge explicitly states that's OK. \$\endgroup\$ – AdmBorkBork Jan 19 '17 at 17:24
  • \$\begingroup\$ Actually missed the point where you can print other data, the challenge creator might need to say if the very-verbose output of Measure-Command is acceptable. also $1=date;-1e3..1e3;(date)-$1 is 2 bytes shorter than the measure-command option there, \$\endgroup\$ – colsw Jan 19 '17 at 17:29
  • \$\begingroup\$ Oh right, haha. Nice golf. \$\endgroup\$ – AdmBorkBork Jan 19 '17 at 17:32

Perl 6, 45 bytes

.put for -($_=1e3)..$_;put (now -INIT now)*$_

Try it


# print the values

.put             # print with trailing newline ( method call on 「$_」 )

for              # for each of the following
                 # ( temporarily sets 「$_」 to the value )

  $_ = 1e3       # store 「Num(1000)」 in 「$_」
..               # inclusive Range object

# print the time elapsed

put              # print with trailing newline

(now - INIT now) # Duration object that numifies to elapsed seconds
* $_             # times 1000 to bring it to milliseconds

# The 「INIT」 phaser runs code (the second 「now」) immediately
# as the program starts.

# There is no otherwise unrelated set-up in this code so this is a
# reliable indicator of the amount of time it takes to print the values.

J, 22 bytes


Try it online!

timex is a builtin that executes the string and returns the time it took to evaluate it in seconds. The string forms the range [-1000, 1000] using i:, then columinizes it using ,., and prints it using the builtin echo.


Pyth, 18 15 14 bytes


Try it here!


This is similar to my python answer.

    J^T3           Set J to 1000
  }_    J         List ranging from -1000 to 1000
j                 Join the list with new lines and implicitly print it
         ;*.d1J   Print time since execution of the program in milliseconds



Noodel, 17 13 bytes

13 bytes

Tried a slightly different approach and saved 4 bytes.


Try it:)

How it works

Ƈ             # Pushes on the amount of milliseconds passed since 01/01/1970.

 Qjȥ          # Pushes 2001 onto the stack.
 Qj           # Pushes on the string "Qj"
   ȥ          # Converts the string into a number as base 98.

    ḶGQɱ⁻Ñ€   # Loops 2001 times printing -1000 to 1000.
    Ḷ         # Consumes the 2001 and loops the following code 2001 times.
     GQ       # Pushes on the string "GQ"
       ɱ      # Pushes on the current counter of the loop (i)
        ⁻     # Subtracts (i - "GQ") since i is a number, the "GQ" is converted to a number which will fail.
              # So, Noodel will treat the string as a base 98 number producing (i - 1000). 
         Ñ    # Consume what is on the top of the stack pushing it to the screen followed by a new line.
          €   # The end of the loop.

           Ƈ⁻ # Calculates the duration of execution.
           Ƈ  # Pushes on the amount of milliseconds passed since 01/01/1970.
            ⁻ # Pushes on (end - start)

17 bytes


Try it:)

How it works

Ƈ                 # Pushes on the amount of milliseconds passed since 01/01/1970.

 GQȥḋɲṡ×2         # Used to create the range of numbers to be printed.
 GQ               # Pushes on the string "GQ".
   ȥ              # Converts the string into number as if it were a base 98 number. (which is 1000)
    ḋ             # Duplicates the number and pushes it onto the stack. 
     ɲ            # Since the item on top is already a number, makes the number negative (random thing I threw in at the very beginning when made the langauge and totally forgot it was there)
      ṡ           # Swaps the first two items on the stack placing 1000 on top.
       ×2         # Doubles 1000 producing... 2000

         Ḷñ⁺1€Ñ   # Prints all of the numbers from -1000 to 1000.
         Ḷ        # Consumes the 2000 to loop the following code that many times (now -1000 is on the top).
          ñ       # Prints the value on top of the stack followed by a new line.
           ⁺1     # Increment the value on top of the stack by 1.
             €    # End of the loop.
              Ñ   # Since 1000 is still not printed, this consumes 1000 and prints it followed by a new line.

               Ƈ⁻ # Calculates the number of milliseconds to execute program.
               Ƈ  # Pushes on the amount of milliseconds passed since 01/01/1970.
                ⁻ # Pushes on (end - start) in milliseconds.
                  # At the end, the top of the stack is pushed to the screen.

The snippet uses the values -4 to 4 in order to not take so long to complete.

<div id="noodel" code="ƇFȥḶAɱ⁻Ñ€Ƈ⁻" input="" cols="10" rows="10"></div>

<script src="https://tkellehe.github.io/noodel/noodel-latest.js"></script>
<script src="https://tkellehe.github.io/noodel/ppcg.min.js"></script>

  • \$\begingroup\$ Did you create this language after or before the challenge? \$\endgroup\$ – Rɪᴋᴇʀ Jan 16 '17 at 0:27
  • \$\begingroup\$ @EasterlyIrk, before:) \$\endgroup\$ – tkellehe Jan 16 '17 at 2:02

TI-Basic, 22 bytes

Disp A
  • Many commands are represented by 1 or 2-byte tokens.

  • Tested on an emulated TI-84 CSE.


Matlab, 16 23 Bytes


Edit: I realised I was violating several of this challenge's rules. That'll teach me to skim read the challenge late at night. I also now realise that the corrected answer is almost identical to the Octave solution, but such is life.

Prints each element in the created linear space array -1000:1000 (the lack of ; prints to console).

tic/toc records the time and toc prints the time to the console with or without the ; . 1e3 is needed to print in milliseconds.

  • \$\begingroup\$ Quite right, a correct solution has been edited. \$\endgroup\$ – Owen Morgan Jan 15 '17 at 17:32

Groovy, 75 73 bytes

(-1000..1e3).each{println it}

Thanks to jaxad0127 for saving 2 bytes!

Try it here !

  • 1
    \$\begingroup\$ nanoTime with a divide by 1e6 is shorter than currentTimeMillis. It also gives fractional time. \$\endgroup\$ – jaxad0127 Jan 15 '17 at 18:45

8th, 61 47 bytes

Thanks to 8th_dev for nice improvement (saved 14 bytes)

d:msec ( . cr ) -1000 1000 loop d:msec swap - .

This will print all the integer numbers between -1000 and 1000 in ascending order and the time taken (in milliseconds) to print these numbers

  • 1
    \$\begingroup\$ It should be noted that suggesting edits to improve code qualifies as "destructive". Suggestions for golfing should be given as a comment instead. I'd ping the user who did it if I could, but I can't. meta.codegolf.stackexchange.com/q/1615/34718 \$\endgroup\$ – mbomb007 Jan 19 '17 at 22:16
  • 1
    \$\begingroup\$ I know that you approved it, which is fine since it's your own post, but other reviewers in the review queue should reject it, and the user who suggested the edit in the first place shouldn't have. \$\endgroup\$ – mbomb007 Jan 19 '17 at 22:17
  • \$\begingroup\$ @mbomb007 - Thank you for letting me know this. In this specific case I verified the code before accepting it, but for the next time I will skip or reject such kind of review. \$\endgroup\$ – Chaos Manor Jan 19 '17 at 22:39

Japt, 23 bytes

There are two equivalent solutions:

Oo(Ð -(A³òA³n @OpXÃ,йn
K=Ð;A³òA³n @OpXÃ;OoÐ -K

The first one basically does the following:

output(-(new Date() - (1000 .range(-1000).map(X => print(X)), new Date())));

That is, the numbers are printed in the middle of the subtraction to avoid having to store the time in a variable. However, it's not any shorter than the variable route, which is basically:

K = new Date(); 1000 .range(-1000).map(X => print(X)); output(new Date() - K);

In the latest version of Japt (newer than this challenge), K is set up to automatically return new Date(). This cuts the first solution down to 21 bytes:

Oo(K-(A³òA³n @OpXÃK)n

Test it online!


QBIC, 34 bytes


Uses the QBasic TIMER function, which returns seconds in decimal notation. Making it look pretty adds some bytes.


d=timer     Set 'd' to the current # seconds since midnight
[-z^3,z^3|  FOR -1000 to 1000  --  Note that 'z' = 10 in QBIC, and z^3 saves a byte over 1000
?a          Display the iterator
]           Close the FOR loop
    timer-d Take the current time minus the time at the start of the program -- 0.156201
?z^3*()     Multiply by a thousand and display it   156.201

C++ - 261

Just for a laugh I thought I'd post a C++ answer.

#include <iostream>
#include <chrono>
using namespace std::chrono; using c=std::chrono::system_clock; void p(){c::time_point n=c::now();for(int i=-1001;++i<1001;)std::cout<<i<<"\n";std::cout<<(duration_cast<milliseconds>(system_clock::now()-n)).count()<<"\n";}

I'll leave it as an exercise to determine what it is doing and how to call it - shouldn't be too difficult.


Scala, 77 bytes

def t=System.nanoTime
val s=t
Range(-1000,1001)map println

ForceLang, 124

set t timer.new()
set i -1000
label 1
io.writeln set i i+1
if i=1000
 io.write math.floor 0.001.mult t.poll()
goto 1

Note: You should supress stderr when running this. I believe the consensus on meta is that this does not incur a byte count penalty.


SimpleTemplate, 92 bytes

What really killed me was the need to record the time.

{@callmicrotime intoX 1}{@for_ from-1000to1000}{@echol_}{@/}{@phpecho microtime(1)-$DATA[X]}

Since there's no math (yet), this makes things pretty hard, forcing me to write PHP directly.


{@call microtime into start_time true}
{@for i from -1000 to 1000 step 1}
    {@echol i}{@// echoes a newline after}
{@php echo microtime(true) - $DATA["start_time"];}


I've ran this with the commit e118ae72c535b1fdbe1b80c847f52aa161854fda, from 2017-01-13.

The latest commit was to fix something that is un-related to the code in here.


C 134 133 bytes

Thanks to @Thomas Padron-McCarthy for saving 1 byte.

f(){clock_t s,e;s=clock();for(int i=-1000;i<1001;i++)printf("%d\n",i);e=clock();printf("%lf",((double)((e-s))/(CLOCKS_PER_SEC))*1e3);}

Ungolfed version :

void f()
  clock_t s,e;


  for(int i=-1000;i<1001;i++)


  • \$\begingroup\$ You can save one character by changing "%lf" to "%f". \$\endgroup\$ – Thomas Padron-McCarthy Jan 16 '17 at 11:37
  • \$\begingroup\$ Why not int t=time(null); ... printf("%d",time(null)-t)? Shorter AFAIK \$\endgroup\$ – SIGSTACKFAULT Jan 17 '17 at 17:07

Gura, 75 bytes


Clojure, 94 bytes

(let[t #(System/currentTimeMillis)s(t)](doseq[n(range -1e3 1001)](println n))(println(-(t)s)))

I'm disappointed at how long this got, but I guess no one ever claimed Clojure was a good language to golf in.

Naïve solution that just records the starting time, loops, then prints the current time minus the starting time. Unless Clojure has a ms-time getter than I'm missing, I don't know how this could get any shorter. Maybe some kind of implicit loop?

(defn time-print []
  (let [t #(System/currentTimeMillis) ; Alias the time-getter to "t"
        start (t)] ; Record starting time
    (doseq [n (range -1000 1001)] ; Loop over the range...
      (println n)) ; ... printing the numbers

    (println (- (t) start)))) ; Then print the current time minus the starting time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.