+ 8

Is there any difference between one liners and lengthy codes other than no. of lines?

I've seen plenty of good codes related to assignments.Some coders code a program in 8-10 lines and others just do it in one line! Both the codes work the same but I want to know the inner details of both codes.

25th Jan 2019, 12:07 PM
Ayush Sinha
Ayush Sinha - avatar
9 Respuestas
+ 20
Some python functions like any(), all() etc. that allow for very compact code are optimized to be executed very quickly and will probably be faster than (for example) a couple of nested loops that do the same job. So in that way, one-liners might be faster than longer code. But that's not a general rule. A compiler/interpreter doesn't really care how much text it has to process (unless your code consists of thousands of lines of code). The time that is needed to parse the code and "translate" it is negligible. Speedwise, a loop like "for i in range(50): print()" isn't faster than using 50 lines of print(), print(), print(). (I just tested it, they take almost the same time, sometimes 50 lines of code are processed even a bit faster than the loop).
25th Jan 2019, 1:13 PM
Anna
Anna - avatar
+ 14
Yes.... Almost always one-liner codes are not-really readable 😁
25th Jan 2019, 12:45 PM
KrOW
KrOW - avatar
+ 10
Most one liners are made by experienced programmers they have a great understanding whats happening under the hood with types,refrences,objects,regex,ternery ect, There not difficult to make but for a beginner they can look intimidating 😰 i wouldent worry about it too much beacuse many of us prefer the long easy to understand codes.
25th Jan 2019, 1:35 PM
D_Stark
D_Stark - avatar
+ 5
Shorter codes are generly preferred, because they require less storage and usually get processed faster. However, sometimes longer codes get processed faster, due to how the computer interprets certain commands. Processing speeds are to do with "inner details", as you say. To find them, you would need to investigate deeply into how a computer reads the code. They would be affected by many things and make a difference only in very complex applications. I would not worry about it until you are quite adept. For now, just focus on making your code adaptable and easy for humans to read -- which usually means short, neatly spaced out, and with functions to avoid repetition.
25th Jan 2019, 12:25 PM
James
James - avatar
+ 5
So, should I happy with my codes? You can check them out.......
25th Jan 2019, 2:42 PM
Ayush Sinha
Ayush Sinha - avatar
+ 1
It's not matter the only things are your code should be readable and it must be optimised for fast processing and less memory
26th Jan 2019, 3:05 AM
Shubham Lad
0
No how
26th Jan 2019, 7:43 PM
Oleg Chistov
Oleg Chistov - avatar
0
the first dud never heard of plagiarism, copy less from google mate
2nd Jan 2023, 3:08 PM
Mohamed Ali Merali
Mohamed Ali Merali - avatar