-
Better sqaure?
I have created a function that draws a square based on user input.
Is there a better way or any ways I can make the code smaller but still work as
efficently?
Code:
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
// function prototype
int draw ( int );
// main function - driver //////////////////////////////////////////////////////
//
int main ( void )
{
int n;
cout << "Enter a side value: ";
cin >> n;
draw ( n );
cin.get(); // freeze console window
cin.ignore();
return 0; // indicate program ended sucsessfuly
}
// function draws a square
int draw ( int n )
{
int counter = 0;
do // makes sure the hieght is correct
{
for ( int i = 1; i <= n; i++ ) // deals with length
{
cout << "*";
}
cout << "\n";
counter++;
} while ( counter < n );
return n;
}
-
maybe you can, but I don't think it will make the program significantly faster... at least not to a level where you can see the difference...
but if you are really eager on making it faster, go if you are using gcc, and in the current directory, there should be a file called source.s ....
learn a bit of assembly, and you may be able to improve it
here's how it looks
Code:
.file "test.c"
.section .rodata
.LC0:
.string "%d"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
andl $-16, %esp
movl $0, %eax
addl $15, %eax
addl $15, %eax
shrl $4, %eax
sall $4, %eax
subl %eax, %esp
subl $8, %esp
leal -12(%ebp), %eax
pushl %eax
pushl $.LC0
call scanf
addl $16, %esp
movl $0, -8(%ebp)
movl $0, -4(%ebp)
jmp .L2
.L5:
subl $12, %esp
pushl $42
call putchar
addl $16, %esp
leal -4(%ebp), %eax
incl (%eax)
.L4:
movl -12(%ebp), %eax
cmpl %eax, -4(%ebp)
jl .L5
subl $12, %esp
pushl $10
call putchar
addl $16, %esp
movl $0, -4(%ebp)
.L2:
movl -12(%ebp), %eax
cmpl %eax, -8(%ebp)
setl %dl
leal -8(%ebp), %eax
incl (%eax)
testb %dl, %dl
jne .L4
movl $0, %eax
leave
ret
.size main, .-main
.ident "GCC: (GNU) 4.0.3 20051201 (prerelease) (Debian 4.0.2-5)"
.section .note.GNU-stack,"",@progbits
-
Thanks for the reply, I think I will give that a shot
-
Could do something like this:
Code:
#include <string>
// [...]
void square( int num )
{
std::string row( num, '*' );
for ( int i=0; i<num; i++ )
std::cout<< row << '\n';
}
don't forget to
-
That would probably be less efficient, because of the overhead of creating a string class.
Code:
void print_square(int size) {
for(int y = 0; y < size; y ++) {
for(int x = 0; x < size; x ++) {
cout << '*';
}
cout << endl;
}
}
I don't see how printing a square in the manner you have done would slow your program down unless you're using a really slow computer. It's likely to be the screen itself that's causing any delay that you can see; if you want to avoid this, try ANSI escape sequences or ncurses.
[edit]Don't forget to end the "don't forget to" sentence! :) [/edit]
-
I've forgotten ... I think I should leave it there until I remember. I didn't read the 'and more efficient' part. Just the part with shorter code.
I'll test it when I get home to see which is quicker.
-
You probably won't be able to measure the speed differences between those programs. 0 ms and 0 ms. It would be better to look at the generated assembly code and compare them (by length, if you don't know assembly). Add the -S option to GCC.
-
Whoops,
using
Code:
void square( int num )
{
std::string row( num, '*' );
for ( int i=0; i<num; i++ )
std::cout<< row << '\n';
}
void print_square(int size) {
for(int y = 0; y < size; y ++) {
for(int x = 0; x < size; x ++) {
std::cout << '*';
}
std::cout << '\n'; // changed from endl to '\n' for testin'
}
}
int main( void )
{
double dwks_square, twomers_square;
TimeIt t;
t.StartTimer();
for ( int i=0; i<1000; i++ )
print_square( 10 );
t.EndTimer();
dwks_square = t.GetTime();
t.StartTimer();
for ( int i=0; i<1000; i++ )
square( 10 );
t.EndTimer();
twomers_square = t.GetTime();
std::cout<< "dwks average: " << dwks_square/1000 << '\n';
std::cout<< "twomers average: " << twomers_square/1000 << '\n';
return 0;
}
with http://cboard.cprogramming.com/showp...4&postcount=10, they're both (dwks' and my functions) very close in speed. ~0.00001 seconds different in one test.