21
\$\begingroup\$

Summary

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:

-1000
-999
-998
-997
...

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++)
        {
            Console.WriteLine(i);
        }
        Console.WriteLine(st.ElapsedMilliseconds);      
    }
}

Restrictions

Standard loopholes are not allowed

Other infos

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

\$\endgroup\$
12
  • \$\begingroup\$ @GurupadMamadapur No, sorry \$\endgroup\$ Jan 14, 2017 at 18:15
  • \$\begingroup\$ Why? I think essentially to print those numbers every statement is involved from the start of the program right? \$\endgroup\$ Jan 14, 2017 at 18:19
  • 1
    \$\begingroup\$ @GurupadMamadapur Ok, you're right, I will edit the question accordingly. \$\endgroup\$ Jan 14, 2017 at 18:32
  • \$\begingroup\$ Can the program wait some amount of time from the start, and print that amount? \$\endgroup\$
    – xnor
    Jan 15, 2017 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\$ Jan 15, 2017 at 10:29

38 Answers 38

1
2
1
\$\begingroup\$

SAS, 36 bytes

Pretty straightforward, SAS prints out the time taken for a data step by default..

data c;do i=-1000 to 1000;put i;end;
\$\endgroup\$
1
\$\begingroup\$

Racket, 72 bytes

(time(map(lambda(x)(display(exact-floor x))(newline))(range -1e3 1001)))
\$\endgroup\$
3
  • \$\begingroup\$ I thought Racket and Scheme were 2 different languages. Is this a polygot? \$\endgroup\$ Jan 18, 2017 at 16:38
  • \$\begingroup\$ @Carcigenicate I somehow agree, Racket is a dialect of Scheme. I will remove "Scheme" from the title. \$\endgroup\$ Jan 18, 2017 at 20:39
  • \$\begingroup\$ Oh, I didn't know that. I thought they were both just independent lisp dialects. \$\endgroup\$ Jan 18, 2017 at 20:45
0
\$\begingroup\$

C# - 123

This isn't going to win anything but hey, here it goes :)

()=>{var t=DateTime.Now;var s="";for(int i=-1000;i<1001;i++)s+=i+"\n";Console.WriteLine(s+(DateTime.Now-t).Milliseconds);};

Run like this:

Action y = ()=>{var t=DateTime.Now;var s="";for(int i=-1000;i<1001;i++)s+=i+"\n";Console.WriteLine(s+(DateTime.Now-t).Milliseconds);};
y();
\$\endgroup\$
0
\$\begingroup\$

Lua, 47 Characters

for i=-1e3,1e3 do print(i)end print(os.clock())

It's nice and simple, a basic printing for loop from -1e3 to 1e3, then just print the os.clock, which is conveniently the ms since program execution.

\$\endgroup\$
0
\$\begingroup\$

Pyth, 19 bytes

Edit: So apparently .d1 is the process time. Saved 6 bytes.

Edit: .d1 is the process time in µseconds, so I have to multiply it by 1000.

K^T3FNr_KhKN;*K.d1

This is a naive solution that pretty much does what's asked without any trick. I'm a beginner with Pyth and still getting the hang out of it. Any suggestion would be very appreciated!

Explanation

K^T3FNr_KhKN;*K.d1
K^T3                     Assigns 10^3 to K
    FNr_KhK              Loops on N from -1000 to 1000
           N;            Each loop, print N
             *K.d1       Prints process time * 1000
\$\endgroup\$
0
\$\begingroup\$

Forth, 60 bytes

utime - Report the current time in microseconds since some epoch.

utime
: f 1001 -1000 do I dup . CR loop ; f
utime - 1000 / .
\$\endgroup\$
0
\$\begingroup\$

Java 7, 125 bytes

void c(){long x=System.nanoTime(),i=-1001;for(;i<1e3;)System.out.println(++i);System.out.println((System.nanoTime()-x)/i/i);}

Ungolfed:

void c() {
  long x = System.nanoTime(),
       i = -1001;
  for(; i < 1e3; ){
    System.out.println(++i);
  }
  System.out.println((System.nanoTime() - x) / i / i);
}

Test code:

Try it here.

class M{
  static void c(){long x=System.nanoTime(),i=-1001;for(;i<1e3;)System.out.println(++i);System.out.println((System.nanoTime()-x)/i/i);}

  public static void main(String[] a){
    c();
  }
}

Example output:

-1000
-999
...
...
...
999
1000
13
\$\endgroup\$
0
\$\begingroup\$

tcl, 58

set i -1001;puts [time {time {puts [incr i]} 2001}]

testable on http://rextester.com/WQVC76600

Note: The answer is given as microseconds per iteration — The iterations here indicated belong to the outermost time command, which are implicitly 1 when its iterations parameter is omitted, which is the case; and not about the loop iterations!

\$\endgroup\$
1
2

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.