Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
embl_hpc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bio-IT Workshops
embl_hpc
Commits
fa6ba653
Commit
fa6ba653
authored
6 years ago
by
Mike Smith
Browse files
Options
Downloads
Patches
Plain Diff
add timer to c program
parent
8b73339e
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
exercises/hpc_example.cpp
+10
-5
10 additions, 5 deletions
exercises/hpc_example.cpp
with
10 additions
and
5 deletions
exercises/hpc_example.cpp
+
10
−
5
View file @
fa6ba653
...
...
@@ -6,6 +6,7 @@
#include
<string.h>
#include
<unistd.h>
#include
<limits.h>
#include
<time.h>
#include
<iostream>
// std::cout, std::endl
#include
<thread>
// std::this_thread::sleep_for
#include
<chrono>
// std::chrono::seconds
...
...
@@ -18,15 +19,15 @@ int main(int argc,char* argv[])
std
::
vector
<
std
::
string
>
sources
;
int
*
a
;
size_t
mem
=
0
;
int
time
=
10
;
int
wait
time
=
10
;
char
hostname
[
HOST_NAME_MAX
];
time_t
t1
,
t2
;
for
(
int
i
=
1
;
i
<
argc
;
++
i
)
{
if
(
std
::
string
(
argv
[
i
])
==
"-t"
)
{
if
(
i
+
1
<
argc
)
{
// Make sure we aren't at the end of argv!
std
::
stringstream
ss1
(
argv
[
++
i
]);
if
(
!
(
ss1
>>
time
))
if
(
!
(
ss1
>>
wait
time
))
std
::
cerr
<<
"Invalid number "
<<
argv
[
i
]
<<
'\n'
;
}
else
{
// Uh-oh, there was no argument to the destination option.
std
::
cerr
<<
"-t option requires one argument."
<<
std
::
endl
;
...
...
@@ -46,19 +47,23 @@ int main(int argc,char* argv[])
}
}
time
(
&
t1
);
int
result
=
gethostname
(
hostname
,
HOST_NAME_MAX
);
std
::
string
hostname2
=
hostname
;
std
::
cout
<<
"Current host is: "
+
hostname2
<<
std
::
endl
;
std
::
cout
<<
"Wait time is: "
<<
time
<<
" seconds"
<<
std
::
endl
;
std
::
cout
<<
"Wait time is: "
<<
wait
time
<<
" seconds"
<<
std
::
endl
;
std
::
cout
<<
"Memory usage is: "
<<
mem
<<
"MB"
<<
std
::
endl
;
long
int
l
=
mem
*
1024
*
1024
;
a
=
(
int
*
)
calloc
(
mem
,
1024
*
1024
);
memset
(
a
,
0
,
mem
*
1024
*
1024
);
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
seconds
(
time
));
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
seconds
(
wait
time
));
free
(
a
);
time
(
&
t2
);
std
::
cout
<<
"Actually running time: "
<<
difftime
(
t2
,
t1
)
<<
" seconds"
<<
std
::
endl
;
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment