소수구하는 구하는 알고리즘 시간 측정하기?

nayana의 이미지

입력을 시키면 블럭된 상태로 빠지게 됩니다.
답변 부탁드리겠습니다. 무엇이 잘못된건가요?^^
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include

#define TRUE 1
#define LOOP 1000

static struct timeval _tstart, _tend;

int is_prime1( int n );
int is_prime2( int n );
void result( int i, int n, int r, long t );
void t_start();
void t_end();
long tval( long t1, long t2 );

int main( void )
{
int n;
long t1, t2;//시간을 측정하기 위한 변수
int r;//결과를 저장하기위한 변수
int i;

puts("\n PRIME2 : Prime algorithm speed test."
"\n Input 0 to end program." );

while( TRUE )
{
puts( "\nInput number to test ->" );
scanf( "%d ", &n );

if( n < 0 )//음수이면 재입력
continue;

if( n == 0 )//0이면 입력되면 종료
break;
/*********************************************
*is_prime1 속도 실행 측정
********************************************/
t_start();

for( i = 0; i < LOOP; i++ )
r = is_prime1( n );

t_end();

result( 1, n, r, tval( t1, t2 ) );

/**********************************************
*is_prime2 속도 실행 측정
*********************************************/
t_start();

for( i = 0; i < LOOP; i++ )
r = is_prime2( n );

t_end();

result( 2, n, r, tval( t1, t2 ) );
}

return 0;
}

/*********************************
*시작시간
*********************************/
void t_start()
{
gettimeofday( &_tstart, NULL );
}

/********************************
*끝나는 시간
********************************/
void t_end()
{
gettimeofday( &_tend, NULL );
}

/****************************************
*시간차
****************************************/
long tval( long t1, long t2 )
{
t1 = _tstart.tv_sec + _tstart.tv_usec;
t2 = _tend.tv_sec + _tend.tv_usec;

return t2 -t1;
}

/*******************************
*첫번째 소수 구하는 알고리즘
*******************************/
int is_prime1( int n )
{
int i;
for( i = 2; i < n; i++ )
if( n % i == 0 )
return !TRUE;
return TRUE;
}

/*******************************
*두번째 소수 구하는 알고리즘
*******************************/
int is_prime2( int n )
{
int i, sqrn;
sqrn = ( int )sqrt( n );
for( i = 2; i <= sqrn; i++ )
if( n % i == 0 )
return !TRUE;
return TRUE;
}
/****************************************
*결과
****************************************/
void result( int i, int n, int r, long t )
{
printf("\n Prime%d Ans : %d is %s prime number in %ld time.",
i, n, r ? "": "not", t );
}

kall의 이미지

nayana wrote:
입력을 시키면 블럭된 상태로 빠지게 됩니다.
답변 부탁드리겠습니다. 무엇이 잘못된건가요?^^

scanf( "%d ", &n ); scanf( "%d", &n );

scanf에 들어간 포맷이 잘못됐네요.

----
자신을 이길 수 있는자는
무슨짓이든 할수있다..
즉..무서운 넘이란 말이지 ^-_-^
나? 아직 멀었지 ㅠㅠ

jenix의 이미지

음.. scanf 에 빈칸이 있어도 그럴수가 있는건가요?.. 그렇군요 :)

근데.. 저는 왜 컴파일이 안되죠;; 음..

리눅스인데.. gcc..

시간관련인듯해서 time.h 를 살펴보니..

    116 /* POSIX.1b structure for a time value.  This is like a `struct timeval' but
    117    has nanoseconds instead of microseconds.  */
    118 struct timespec
    119   {
    120     __time_t tv_sec;        /* Seconds.  */
    121     long int tv_nsec;       /* Nanoseconds.  */
    122   };
    123

로 되어있군요..

---------------------------------------------------------------------------
http://jinhyung.org -- 방문해 보세요!! Jenix 의 블로그입니다! :D

kall의 이미지

jenix wrote:

근데.. 저는 왜 컴파일이 안되죠;; 음..

리눅스인데.. gcc..

man 2 gettimeofday를 해보면

#include <sys/time.h>
#include <unistd.h>
두개의 파일을 포함하라고 나옵니다. timeval도 그 안에 정의되어 있구요..

----
자신을 이길 수 있는자는
무슨짓이든 할수있다..
즉..무서운 넘이란 말이지 ^-_-^
나? 아직 멀었지 ㅠㅠ

nayana의 이미지

감사합니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.