Fastest N times Hello World (Java/C/ C++)

-5

Not a new one:

Input N, being n, 0 ≤ n ≤ 5.

Output N times "Hello World."

Example:

Input

3

Output

Hello World.
Hello World.
Hello World.

These 2 are my best solutions:

First

public class Out 
{ 
    public static void main(String[] a) throws Exception 
    { 
            java.io.OutputStream o = System.out; 
            byte n = Byte.parseByte(new java.io.BufferedReader(new java.io.InputStreamReader(System.in)).readLine()); 
            for(byte i = 0;i<n;i++){
                o.write("Hola mundo.\n".getBytes());
            }
    } 
}

Second

public class Out 
{
    public static void main(String[] a) throws Exception{ 
        byte n = Byte.parseByte(new java.io.BufferedReader(new java.io.InputStreamReader(System.in)).readLine()); 
        while(n>0){switch(n){case 0: w();break;case 1: w();break;case 2: w();break;case 3: w();break;case 4: w();break;case 5: w();break;}n--;}
    }

    static void w() throws Exception{
        System.out.write("Hola mundo.\n".getBytes());
    }
}

According to the server it is being tested on ( im not an owner of that server), first attempt gives me 0.006ms and 622 KiB while second attempt gives me same time with 625 KiB.

I am not able to provide the Java version being used but if you want, feel free to use the main site ( spanish language ) https://www.aceptaelreto.com/problem/statement.php?id=116

P.S.: Some peoeple were able to get it on 0.005ms and 88KiB.

edit: -this code is being benchmarked on the link used before. -The point is to get the fastest code ( and if possible, with the least amount of memory used), not the shortest one.

Apparently Im not able to use comments since I do not have the required reputation score.

Looking4Tips

Posted 2019-11-07T11:29:08.347

Reputation: 9

Question was closed 2019-11-08T15:02:58.673

1How did you benchmark your code? – stephanmg – 2019-11-07T11:50:49.137

8Is it [tag:fastest-code] as the title and body suggests, or [tag:code-golf] as you've tagged it? – Jo King – 2019-11-07T12:20:01.807

8Do you understand that of the 6ms all but a few microseconds is the program loading overhead? – my pronoun is monicareinstate – 2019-11-07T13:20:29.900

This is a poor-quality question per @someone's comment above, and I've downvoted it, but it's not closeworthy, either as unclear or for lacking a winning criterion. – pppery – 2019-11-07T20:08:18.597

Yeah I think it should not be closed. But just improve the question. – stephanmg – 2019-11-08T16:10:50.450

Answers

1

C++11 approach (Time: 3.6 µs on some Intel i7, File size of a.out: 28K) compiled via:

clang -O3 -march=native -Ofast golf.cpp: (Clang 6.1)

#include <iostream>
#include <chrono>
#include <sstream>

int main(int argc, char** argv) {
  auto t1 = std::chrono::high_resolution_clock::now();
  const int n = 5;
  if (n < 0 || n > 5) return 1;
  std::stringstream ss;
  for (size_t i = 0; i < n; i++) ss << "Hello world\n";
  std::cout << ss.str();
  auto t2 = std::chrono::high_resolution_clock::now();
  auto duration = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
  std::cout << duration << "µs" << std::endl;
  return 0;
}

stephanmg

Posted 2019-11-07T11:29:08.347

Reputation: 261

One could add the check for the n you specified, but this feels boring. – stephanmg – 2019-11-07T12:03:28.820

1

Using C (size <12 KiB, time < 0.001s)

$ cat >hola.c <<END_C
#include<unistd.h>
int main(){
  int i,n;
  read(0,&n,1);
  for(i='0';i<n;++i){
    write(1,"Hola mundo.\n",12);
  }
}
END_C

$ gcc hola.c -o hola

$ wc -c hola
11944 hola

$ time ./hola <<<5
[...]
real    0m0.001s
user    0m0.000s
sys     0m0.000s

Nahuel Fouilleul

Posted 2019-11-07T11:29:08.347

Reputation: 5 582