Time taken to print numbers

21

1

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.

Horváth Dávid

Posted 2017-01-14T16:30:16.127

Reputation: 679

@GurupadMamadapur No, sorry – Horváth Dávid – 2017-01-14T18:15:11.327

Why? I think essentially to print those numbers every statement is involved from the start of the program right? – Gurupad Mamadapur – 2017-01-14T18:19:04.403

1@GurupadMamadapur Ok, you're right, I will edit the question accordingly. – Horváth Dávid – 2017-01-14T18:32:26.693

Can the program wait some amount of time from the start, and print that amount? – xnor – 2017-01-15T01:54:05.703

@xnor I think, that would change the challenge, and because there are already lots of answers to the original challenge, I would say no. – Horváth Dávid – 2017-01-15T10:29:55.270

Can I write an answer in Arduino? – SIGSTACKFAULT – 2017-01-17T17:01:30.767

@Blacksilver Yes, why not? – Horváth Dávid – 2017-01-17T19:32:26.553

Arduino has a built-in for time since start. Muahaha! – SIGSTACKFAULT – 2017-01-17T20:53:40.407

@Blacksilver I think, that's fine. – Horváth Dávid – 2017-01-17T21:46:50.343

Can the time output contain anything else besides the raw time in milliseconds? Would "Time taken: 500ms" be ok? – Carcigenicate – 2017-01-18T16:39:57.393

@Carcigenicate Yes, it would be ok. – Horváth Dávid – 2017-01-18T20:28:40.953

@HorváthDávid Oh good. Manually handling time is like a third of my answer. – Carcigenicate – 2017-01-18T20:46:42.057

Answers

9

MATL, 13 bytes

1e3t_y&:!DZ`*

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

Luis Mendo

Posted 2017-01-14T16:30:16.127

Reputation: 87 464

2Very nice! Smart to implement: Implicitly start timer. Was that there from day one, or is that the result of an earlier challenge? – Stewie Griffin – 2017-01-14T20:30:32.853

@StewieGriffin Not from day one. I added it on 13 Jul 2016, probably after having to intiallize it explicitly in a couple of challenges

– Luis Mendo – 2017-01-15T02:05:38.300

9

Octave, 46 43 36 30 23 bytes

tic;(-1e3:1e3)',toc*1e3

This will print:

ans =

  -1000
   -999
   -998
   -997
   -996
   -995

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

tic;disp((-1e3:1e3)'),toc*1e3

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

Explanation:

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

Stewie Griffin

Posted 2017-01-14T16:30:16.127

Reputation: 43 471

8

JavaScript, 60 bytes

(c=console).time();for(i=~1e3;i++<1e3;c.log(i));c.timeEnd();

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).

nicael

Posted 2017-01-14T16:30:16.127

Reputation: 4 585

i=~1e3 to save a byte :-) – ETHproductions – 2017-01-14T18:29:00.360

7

Python 3.5, 80 77 73 bytes

import time
*map(print,range(-1000,1001)),
print(time.process_time()*1e3)

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!

Gurupad Mamadapur

Posted 2017-01-14T16:30:16.127

Reputation: 1 791

7

CJam, 18 bytes

es2001{1e3-n}/es\-

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.

Dennis

Posted 2017-01-14T16:30:16.127

Reputation: 196 637

5

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

EDITS:

  • 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 !).

Golfed

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

Try It Online !

Sidenote

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.

zeppelin

Posted 2017-01-14T16:30:16.127

Reputation: 7 884

4

Bash + GNU utils, 43

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

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

Digital Trauma

Posted 2017-01-14T16:30:16.127

Reputation: 64 644

1You can save two bytes by replacing 1000 with 1e3. – Dennis – 2017-01-14T18:27:57.760

"But the output of time gives ...." ...perhaps you should man bash. – H Walters – 2017-01-14T19:08:19.810

1You can probably save a few bytes, by capturing the date with ms precision directly, like this: date +%s%3N. – zeppelin – 2017-01-14T22:14:07.740

4

JavaScript (ES6), 63 59 bytes

for(c=console.log,i=~1e3;i<1e3;c(++i));c(performance.now())

George Reith

Posted 2017-01-14T16:30:16.127

Reputation: 2 424

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) – ETHproductions – 2017-01-14T18:28:14.510

@ETHproductions thanks :) the ~1e3 is a great touch. – George Reith – 2017-01-14T18:33:55.307

1In the snippet output is from only 952 to 1000 why is that? – Gurupad Mamadapur – 2017-01-14T18:54:54.270

@GurupadMamadapur The snippet output is limited to 50 lines. (Or more precisely: the 50 last lines.) – Arnauld – 2017-01-14T20:10:49.510

@Arnauld Oh okay. – Gurupad Mamadapur – 2017-01-14T20:14:51.097

Why not use for(c=console.log,i=~1e3;i<1e3;c(++i));c(performance.now()) (59 bytes)? According to MDN, the performance.now() "returns a DOMHighResTimeStamp, measured in milliseconds, accurate to one thousandth of a millisecond". Also, "The returned value represents the time elapsed since the time origin". In your case, the "time origin" will be 0 (the time the code started), since it is the only javascript code in the page. In few words: returns the time offset since the page has loaded.

– Ismael Miguel – 2017-01-16T15:11:44.970

1@IsmaelMiguel Amazing wasn't aware of performance.now() or the Performance interface at all – George Reith – 2017-01-16T20:01:48.697

It is relativelly new. You really should read about it. It's extremelly useful. – Ismael Miguel – 2017-01-16T21:14:47.837

4

R, 42 bytes

system.time(cat(-1e3:1e3,sep="\n"))[3]*1e3

This will print

.
.
.
998
999
1000
elapsed 
     60 

To remove elapsed, two additional bytes are necessary:

system.time(cat(-1e3:1e3,sep="\n"))[[3]]*1e3

Sven Hohenstein

Posted 2017-01-14T16:30:16.127

Reputation: 2 464

3

PHP, 110 70 bytes

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

for($t=($m=microtime)($i=~1e3);$i++<1e3;)echo"$i
";echo($m(1)-$t)*1e3;

prints float. Run with -r.

Titus

Posted 2017-01-14T16:30:16.127

Reputation: 13 814

2You can pass microtime() a truthy value and it will return a float directly, you don't have to add the strings. – Alex Howansky – 2017-01-14T21:18:37.687

-30% with that hint. I wish I could do ten upvotes on your comment. :D – Titus – 2017-01-15T15:09:31.717

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! – Ismael Miguel – 2017-01-16T15:24:33.467

@IsmaelMiguel I think JavaScript doesn´t have microseconds. :) – Titus – 2017-01-16T15:49:36.557

@Titus What I meant is that Javascript's dates are all handled in milliseconds, while PHP only had seconds or microseconds. – Ismael Miguel – 2017-01-16T16:05:06.517

3

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.

Billywob

Posted 2017-01-14T16:30:16.127

Reputation: 3 363

Can proc.time be stored in a variable? t=proc.time;x=t(); ... – AnnanFay – 2017-12-31T00:34:09.420

3

Mathematica, 51 bytes

p[1*^3#]&@@AbsoluteTiming@Array[p=Print,2001,-1*^3]

Explanation

Array[p=Print,2001,-1*^3]

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.

JungHwan Min

Posted 2017-01-14T16:30:16.127

Reputation: 13 290

Argh, you beat me by 3 minutes! :) Are you sure Timing doesn't satisfy the (slightly vague) problem description as well as AbsoluteTiming? – Greg Martin – 2017-01-14T20:26:54.943

2@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. – JungHwan Min – 2017-01-14T21:28:43.397

3

Powershell, 27 Bytes

$1=date;-1e3..1e3;(date)-$1

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

Outputs result like:

994
995
996
997
998
999
1000

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:

$1=date;-1e3..1e3;((date)-$1).TotalMilliseconds

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

colsw

Posted 2017-01-14T16:30:16.127

Reputation: 3 195

You could just pipe the range to oh (Out-Host) which will bypass the fact that Measure-Command captures the pipeline. Example at TIO

– AdmBorkBork – 2017-01-19T17:14:42.013

@AdmBorkBork the point was that I don't think it will save bytes, unless I'm missing something. – colsw – 2017-01-19T17:23:05.560

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. – AdmBorkBork – 2017-01-19T17:24:35.307

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, – colsw – 2017-01-19T17:29:57.003

Oh right, haha. Nice golf. – AdmBorkBork – 2017-01-19T17:32:29.100

Somehow worked out in the end! thanks for that, I would have been bringing shame to all the power shell golfers if this was 15 needless bytes longer. – colsw – 2017-01-19T17:33:59.640

2

Perl 6, 45 bytes

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

Try it

Expanded:

# 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.

Brad Gilbert b2gills

Posted 2017-01-14T16:30:16.127

Reputation: 12 713

2

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

-1000
-999
-998
-997
...
997
998
999
1000
4

Chaos Manor

Posted 2017-01-14T16:30:16.127

Reputation: 521

1

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. http://meta.codegolf.stackexchange.com/q/1615/34718

– mbomb007 – 2017-01-19T22:16:04.973

1I 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. – mbomb007 – 2017-01-19T22:17:43.140

@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. – Chaos Manor – 2017-01-19T22:39:04.703

2

Pyth, 18 15 14 bytes

j}_J^T3J;*.d1J

Try it here!

Explanation

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

Edits:

Gurupad Mamadapur

Posted 2017-01-14T16:30:16.127

Reputation: 1 791

I tried this, 14 bytes, not sure if it works right. You need to add a newline at the start of the program. http://pyth.herokuapp.com/?code=%0AM%7D_J%5ET3J%2a.d1J&debug=0

– busukxuan – 2017-01-14T19:54:53.543

2

J, 22 bytes

1e3*timex'echo,.i:1e3'

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.

miles

Posted 2017-01-14T16:30:16.127

Reputation: 15 654

2

Groovy, 75 73 bytes

t=System.&nanoTime
s=t()
(-1000..1e3).each{println it}
print((t()-s)/1e6)

Thanks to jaxad0127 for saving 2 bytes!

Try it here !

Gurupad Mamadapur

Posted 2017-01-14T16:30:16.127

Reputation: 1 791

1nanoTime with a divide by 1e6 is shorter than currentTimeMillis. It also gives fractional time. – jaxad0127 – 2017-01-15T18:45:37.883

2

Matlab, 16 23 Bytes

tic;(-1e3:1e3)'
toc*1e3

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.

Owen Morgan

Posted 2017-01-14T16:30:16.127

Reputation: 221

Quite right, a correct solution has been edited. – Owen Morgan – 2017-01-15T17:32:17.473

2

Noodel, 17 13 bytes

13 bytes

Tried a slightly different approach and saved 4 bytes.

ƇQjȥḶGQɱ⁻Ñ€Ƈ⁻

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

ƇGQȥḋɲṡ×2Ḷñ⁺1€ÑƇ⁻

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>

tkellehe

Posted 2017-01-14T16:30:16.127

Reputation: 605

Did you create this language after or before the challenge? – Rɪᴋᴇʀ – 2017-01-16T00:27:38.387

@EasterlyIrk, before:) – tkellehe – 2017-01-16T02:02:52.153

2

TI-Basic, 22 bytes

startTmr
For(A,-ᴇ3,ᴇ3
Disp A
End
startTmr-Ans
  • Many commands are represented by 1 or 2-byte tokens.

  • Tested on an emulated TI-84 CSE.

Julian Lachniet

Posted 2017-01-14T16:30:16.127

Reputation: 3 216

2

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!

ETHproductions

Posted 2017-01-14T16:30:16.127

Reputation: 47 880

1

QBIC, 34 bytes

d=timer[-z^3,z^3|?a]?z^3*(timer-d)

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

Explanation

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

steenbergh

Posted 2017-01-14T16:30:16.127

Reputation: 7 772

1

Racket, 72 bytes

(time(map(lambda(x)(display(exact-floor x))(newline))(range -1e3 1001)))

Sven Hohenstein

Posted 2017-01-14T16:30:16.127

Reputation: 2 464

I thought Racket and Scheme were 2 different languages. Is this a polygot? – Carcigenicate – 2017-01-18T16:38:25.397

@Carcigenicate I somehow agree, Racket is a dialect of Scheme. I will remove "Scheme" from the title. – Sven Hohenstein – 2017-01-18T20:39:33.787

Oh, I didn't know that. I thought they were both just independent lisp dialects. – Carcigenicate – 2017-01-18T20:45:03.610

1

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.

original.legin

Posted 2017-01-14T16:30:16.127

Reputation: 31

1

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;

  s=clock();

  for(int i=-1000;i<1001;i++)
    printf("%d\n",i);   

  e=clock();
  printf("%f",((double)((e-s))/(CLOCKS_PER_SEC))*1e3);

 }

Abel Tom

Posted 2017-01-14T16:30:16.127

Reputation: 1 150

You can save one character by changing "%lf" to "%f". – Thomas Padron-McCarthy – 2017-01-16T11:37:26.950

Why not int t=time(null); ... printf("%d",time(null)-t)? Shorter AFAIK – SIGSTACKFAULT – 2017-01-17T17:07:15.583

1

Scala, 77 bytes

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

jaxad0127

Posted 2017-01-14T16:30:16.127

Reputation: 281

1

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()
 exit()
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.

SuperJedi224

Posted 2017-01-14T16:30:16.127

Reputation: 11 342

1

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.

Ungolfed:

{@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"];}

Disclaimer:

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.

Ismael Miguel

Posted 2017-01-14T16:30:16.127

Reputation: 6 797

1

Gura, 75 bytes

t=datetime.now();println(-1000..1000);print((datetime.now()-t).usecs/1000);

Sygmei

Posted 2017-01-14T16:30:16.127

Reputation: 1 137

1

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.

Carcigenicate

Posted 2017-01-14T16:30:16.127

Reputation: 3 295

1

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;

J_Lard

Posted 2017-01-14T16:30:16.127

Reputation: 351

0

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();

Carra

Posted 2017-01-14T16:30:16.127

Reputation: 301

0

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.

ATaco

Posted 2017-01-14T16:30:16.127

Reputation: 7 898

0

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!

sergiol

Posted 2017-01-14T16:30:16.127

Reputation: 3 055

0

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

Nepho

Posted 2017-01-14T16:30:16.127

Reputation: 121

0

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 / .

mbomb007

Posted 2017-01-14T16:30:16.127

Reputation: 21 944

0

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

Kevin Cruijssen

Posted 2017-01-14T16:30:16.127

Reputation: 67 575