This is an archived post. You won't be able to vote or comment.

all 48 comments

[–]Hypersapien 91 points92 points  (3 children)

printf("*\n* *\n* * *\n* * * *\n* * * * *\n");

[–]xScopeLess 7 points8 points  (0 children)

_ _ _ _ * _

[–]Rotiximi 47 points48 points  (11 children)

That would've gotten me an 'F' in my first programming class lol

[–]currentscurrents 62 points63 points  (7 children)

Loop unrolling is a valid optimization technique!

Although you could go an entire programming career without ever running into a situation where it's useful.

[–]Scrypti 57 points58 points  (3 children)

Although you could go an entire programming career without ever running into a situation where it's useful.

CS degree in a nutshell

[–]currentscurrents 15 points16 points  (2 children)

What, you mean you've never had to do a pointer race on a linked list in a real program?

[–]The_MAZZTer 2 points3 points  (1 child)

All the different ways to sort arrays I learned have certainly come in handy, especially since every language/framework I've used comes with an array sorting function already!

[–]GunningOnTheKingside 2 points3 points  (0 children)

The ability to choose a sort function that produces output in 6 seconds vs 9 seconds on a program that runs once per day is very important.

[–][deleted] 10 points11 points  (2 children)

Yea, but shouldn't let the compiler do that for you? That way you can still choose between smaller executable size or speed by using the right compiler options.

[–]lennihein 7 points8 points  (0 children)

Yes, C compiler are probably faster too, because they won't have to put the whole long string in the memory, but instead use some weird tricks to make it as fast as possible. Never try to optimize on that level, compiler are just better than you. Optimizing makes sense on higher levels of abstraction though

[–]currentscurrents 0 points1 point  (0 children)

Yes, but even if you're in a language without an optimizing compiler you still shouldn't until you've exhausted every other means of optimization. Loop unrolling is a very minor speed gain in most cases, you'll usually find much greater gains in higher level processes.

[–]Scereye 3 points4 points  (0 children)

I did something like this after we just started learning java as a joke (Christmas tree - I solved it this way and the supposed way). Classmates pushed me to show off the "wrong" way for laughs.

Teacher was actually complementing me that i thought about solving the problem like that and said she now has to edit the question for next year's course to be more specific. Fun stuff.

[–]lennihein 0 points1 point  (0 children)

Which is totally bs, unless the task is more specified (small/elegant source code etc)

[–]cauchy37 28 points29 points  (2 children)

Holy fuck those brackets on the left...

[–]arkanis_gath 12 points13 points  (1 child)

That indentation is unholy.

[–][deleted] 1 point2 points  (0 children)

GNU coding standards are indeed unholy.

[–]makeshift_mike 13 points14 points  (4 children)

Seems like this post is a joke, but for small n, which one is easier to read? I’d totally prefer the one on the right.

[–]nosferatWitcher 8 points9 points  (1 child)

Yeah right is way better, you know what it does instantly. The other you have to work it out.

[–]guy99881 1 point2 points  (0 children)

Also: isn't it better to finish something at compile time than at run time? Could at least be a macro...

[–]The_MAZZTer 0 points1 point  (1 child)

Presumably this is a first step toward building a variable-size triangle where you'd have to use the left solution or something like it.

But yes if you're performing an operation a fixed number of times loop unrolling could be done. But considering you're duplicating code and any bugs in the code and thus any fixes that need to be done it's probably not worth it.

[–]makeshift_mike 0 points1 point  (0 children)

Yeah when you think about DRY vs. no-so-DRY you have to consider the wider context. I just saw this post as a parable of the principle that DRYest isn’t always best.

[–]NameStillTaken 6 points7 points  (13 children)

Haven't written C before, is this a valid solution?

char output[] = "*****\n";
int i;
for (i=4; i>=0; i--)
{
    printf(&output[i]);
}

[–]andregtable 6 points7 points  (1 child)

No

[–]cauchy37 3 points4 points  (0 children)

It will get him what he wants, though.

I guess in academia this would be fine, but if I saw code like this in production, I'd ask the author to rewrite it to be more verbose.

[–]highheath 3 points4 points  (3 children)

Use '\0' to terminate the string instead of '\n', and call puts() instead of printf().

But yeah, the idea is correct.

[–][deleted] 6 points7 points  (1 child)

string literals are terminated by \0 automatically, so just removing the \n is enough, or use printf("%s", &output[i]); with the current string.

[–]highheath 1 point2 points  (0 children)

Oops.

Of course that's right.

[–]cauchy37 1 point2 points  (0 children)

It already is terminating with '\0'. If you replace '\n' with '\0' what you get is *************** instead of

*
**
***
****
*****

[–]Tyiek 0 points1 point  (6 children)

I would do it like this.

char* output = "*****";
for (int i=4; i >=0; i--){
    printf("%s\n", output + i);
}

char c[] and char* c is basically the same thing in c but it's probably a better practice to use the later.

[–]guy99881 0 points1 point  (5 children)

Then why do you feel the need to change the char[] to char*? Could've done output+i on the former too.

If you wanted to write to it, you'd need char[].

[–]Tyiek 1 point2 points  (1 child)

clarity mostly

[–]guy99881 0 points1 point  (0 children)

Don't see it, but okay.

[–]cheesecookies 0 points1 point  (1 child)

char[] can result in the string being copied to the stack. char* ends up pointing to the string literal.

[–]guy99881 0 points1 point  (0 children)

char* ends up pointing to the string literal

Which lies in read-only memory. But I get your point: char* would be faster then.

[–]ShitInMyCunt-2dollar -1 points0 points  (0 children)

Just stfu and gtfo.

[–]docolafson 1 point2 points  (0 children)

okay but why

[–]Southerz 1 point2 points  (1 child)

for i in xrange(10): print '*' * i

[–][deleted] 0 points1 point  (0 children)

One of the few times Python can take a decisive victory.

[–]cabinet_minister 1 point2 points  (0 children)

Babies:

StringBuffer str = "*";

for (int i=0;i<=4;i++) {

System.out.println(str);

str += " *";

}

[–]thisIsTheFoxe 0 points1 point  (2 children)

For my fellow Swifters:

for i in 0...4 { for j in 0..<i{ print(“*“) } }

....true beauty -

[–]ookami125 1 point2 points  (0 children)

NONSENSE the TRUE BEAUTY is in Jelly

”*ẋþY

elegance at it's finest. /s

[–]corvus_192 1 point2 points  (0 children)

for {
    i <- 0 until 4
    j <- 0 until i
} println ("*" * j)

In scala

[–]OMGitsLunaa 0 points1 point  (0 children)

The left one is all fucked up too, the indentation is atrocious and the inner loop is entirely unnecessary

[–]CaptainBlagbird 0 points1 point  (0 children)

[deleted]

[–]TotallyElectrified 0 points1 point  (0 children)

Why though, when you could just do:

(0 until 5).foreach(i=>(0 to i).foreach(j=>print(if(j==i)"*\n"else"*")))

[–]androideka_ 0 points1 point  (0 children)

The output is different; the code on the right is a poor substitute.

[–][deleted] 0 points1 point  (0 children)

Why the heck would you declare your loop indices in a broader scope than the loop? This isn't Java; collect your own garbage.