|
Hi, I wrote a program in Windows CE platform, CPU is ARM 427MHZ.
when program read a text file(size about 700KB,1000 lines) in Flash memory, it needs about 25 seconds to load the file.
is there a means to decrease the reading time?
file.Open(fileName,CFile::typeBinary | CFile::modeRead);
while ( UNReadString(&file,strLine) ) {
strLine.Trim();
strNum = strLine.Tokenize(TEXT("\t"),tokenPos);
strEng = strLine.Tokenize(TEXT("\t"),tokenPos); strCh = strLine.Tokenize(TEXT("\t"),tokenPos);
strRus = strLine.Tokenize(TEXT("\t"),tokenPos);
handleStr();
tokenPos = 0;
}
BOOL UNReadString(CFile *iFile, CString &strReturn)
{
TCHAR tc;
CString strBuff = _T("");
strReturn = _T("");
while( ( sizeof(TCHAR) ) == ( iFile->Read(&tc, sizeof(TCHAR))) ) {
strBuff = tc;
if(_T("\n") != strBuff) {
strReturn += strBuff;
}
else {
return TRUE; }
}
return FALSE;
}
modified 11-Nov-14 12:55pm.
|
|
|
|
|
econy wrote: file.Open(fileName,CFile::typeBinary | CFile::modeRead); Why do you open a textfile in binary mode? You will also find that your use of Trim and Tokenize on each line will add considerably to the time.
|
|
|
|
|
Since there are east asian words in the file, and I try to comment tokenize block, no update
|
|
|
|
|
What does UNReadString do, I cannot find a definition for it?
|
|
|
|
|
had added code for:
UNReadString(CFile *iFile, CString &strReturn)
|
|
|
|
|
Sorry, I was not clear; I meant that I could not find a definition for that function in MSDN, so I do not know what it does. Is this a standard part of WindowsCE and if so where is the documentation for it?
|
|
|
|
|
It is not a Wince or Windows function. I wrote it myself.
|
|
|
|
|
Then that may well be the place where all your time is being used up.
|
|
|
|
|
The first thing you've got to do is work out whether it's I/O bound or whether it's the data processing that's slowing things down. You might find that CFile isn't particularly well tuned for reading from flash and not buffering enough. If it turns out that it's the I/O that's causing the problem find the lowest level function you can call and see what the raw I/O speed of your device is for a range of buffer sizes and then implement something that hits the sweet spot in terms of buffer size. It's a shame you're using MFC classes and not standard library ones as it's a lot easier to replace the underlying buffering and I/O mechanism, but such is life!
Another thing might be to try reading more data in your while loop (say 512 characters at a time) and manually parcelling out the data rather than letting CFile::Read do it.
If it turns out that the I/O is fast enough then have a look at what you're doing with the data. If you've got a lot of string handling you might find loads of temporaries flying about (especially if you're using VC++ 2008 and earlier) and there's loads of hidden memory manipulation that's slowing things down. In that case look at reducing the number of temporaries you need by reusing objects and expoiting things like NRVO.
Anyway, the main thing though is to find out where the bottle neck is then do something about it.
|
|
|
|
|
I have written a program in c++ to check whether a number is prime or not.... I want to know if this is optimized or not..
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter a number:";
cin>>num;
if(num%6==1 || num%6==5 || num==2 || num==3)
{
cout<<"\n"<<num<<" is a prime number";
}
else
{
cout<<"\n"<<num<<" is not a prime number";
}
return 0;
}
Please help me out...
|
|
|
|
|
If you start programming, lesson number one : do not ask whether something is optimized or not without having performance target in mind.
People that ask you to optimize your code without reason just want to waste your time on trivia.
Takes 1 000 000 random numbers. Then measure how much times it takes to find out whether they are prime on your machine.
Then see if you can make it better with random changes and observe.
|
|
|
|
|
I want to know that this program is better than those which are solved using loops.....????
|
|
|
|
|
only way to know it is to do what I told you just above.
This not only depends on the code but on the compiler. A coder can't tell you without benchamrking.
|
|
|
|
|
"Optimized" is not the correct category here. It is simply wrong. 25%6=1, but 25 is not prime.
|
|
|
|
|
Windows 7, VS 2008, MFC, Dialog, c++
I would like to use a control of type Net Address control to provide a method for the user to enter an IP address. Here is the closest I can get:
void CTCP_Client_ConsumerDlg::OnBnClickedButton1()
{
NC_ADDRESS y;
PNC_ADDRESS a = &y;
a->pAddrInfo = 0;
a->PortNumber = 0;
a->PrefixLength = 0;
HRESULT x;
DWORD ip_address = 0;
x = c_net_work_address_edit_box.GetAddress( a );
ip_address = c_net_work_address_edit_box.GetAddress( a );
}
Item: c_net_work_address_edit_box was created by right clicking on the address control and creating a control variable. When I type in: 192.168.2.3 the expected value is not present.
Also: The edit box does not provide the dots between the four parts of the IP address. I have seen that used many places, but want to implement in my code now.
Edit: Answer: Look in section Dialog Editor of the dialog toolbox to find IP Address Control. I don't know what the Network Address Control is for but I lack sufficient knowledge to get it working for my needs.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
modified 19-Oct-14 18:21pm.
|
|
|
|
|
|
I am not doing well with this. There must be something that I have completely missed and just cannot see. Here is my latest effort.
void CTCP_Client_ConsumerDlg::OnBnClickedButton1()
{
NC_ADDRESS nc_address;
PNC_ADDRESS p_nc_address = &nc_address;
p_nc_address->pAddrInfo = 0;
p_nc_address->PortNumber = 0;
p_nc_address->PrefixLength = 0;
HRESULT hr;
DWORD ip_address = 0;
DWORD &rip_address = ip_address;
hr = c_net_work_address_edit_box.GetAddress( p_nc_address );
ip_address = c_net_work_address_edit_box.GetAddress( p_nc_address );
int count = c_net_work_address_edit_box.GetAddress( rip_address );
}
I dropped a Network Address Control on the MFC dialog, then right clicked and added variable
c_net_work_address_edit_box.
The page you suggested has:
int GetAddress(
BYTE& nField0,
BYTE& nField1,
BYTE& nField2,
BYTE& nField3
);
int GetAddress(
DWORD& dwAddress
);
Which I see is the same as my last attempt. But the compiler complains:
Quote: error C2664: 'CNetAddressCtrl::GetAddress' : cannot convert parameter 1 from 'DWORD' to 'PNC_ADDRESS'
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
Having thought about this some more, I think there is an easier way. You should really be using the UpdateData [^] function to exchange information between the class variables and the dialog box. You just need to initialise the variable (if necessary) before calling DoModal on the dialog. Then you (i.e. the user) fill in the values in your various boxes and click OK, and the data is transferred into the variables. If you want interim values saved by pressing some other button, then you need to call UpdateData(TRUE) in that button's click handler. You may also need to modify the data exchange routines for some complex controls, so check the documentation for the control you are using.
|
|
|
|
|
I am not understanding the reply.
Quote: Having thought about this some more, I think there is an easier way. You should really be using the UpdateData[^] function to exchange information between the class variables and the dialog box. You just need to initialise the variable (if necessary) before calling DoModal on the dialog.
The new project is an MFC dialog and the net address control has been dropped on to the dialog. Why would I be calling DoModal?
Maybe I'm barking up the wrong tree. I have seen and used many apps where I enter an IP address that display a small window with four fields of up to three digits, each field separated by dots. How is that display created and put in the dialog?
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
bkelly13 wrote: Why would I be calling DoModal? If it's an MFC dialog then that is effectively how it is run. The dialog is displayed on the screen, the user fills in the data and presses OK. At this point the code in the OnOK override captures the data from the dialog window and saves it in the class object's variables by a call to UpdateData(true) .
I think you would be better with the CIPAddressCtrl Class[^], which is added to the dialog in the same way, as far as I am aware.
|
|
|
|
|
Quote: If it's an MFC dialog then that is effectively how it is run.
When I drop a control onto a dialog, there has never been a need to call DoModal for that control. I tried dropping the Net Address Control onto the dialog and it did not work out well.
EDIT
After spending a couple of hours on this I managed to get the "IP Address Control" to display in the dialog tool box. Its under the Dialog Editor section of the toolbox. Now that I can drop that on to the dialog, my extended question about manually creating the dialog is no longer needed. (That would be the long reply that I am deleting and replacing with this.) Now that the dialog is there, adding a variable and getting the address is no trouble.
So thank you for your patience.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
modified 19-Oct-14 18:18pm.
|
|
|
|
|
dear Sir/madam,
I am pretty much familiar about working with c. Recently, I am supposed to do a project on serial port communication between two PCs using RS232 protocol with c. I am unfamiliar with the keyword and syntax required to accompany this task..kindly help with a tutorial on it
|
|
|
|
|
|
How do you know the poster is using Microsoft's tool or platform? Plenty of other compilers and platforms, and a C developer is more likely to be working on Unix variant or embedded system these days. I'd skip the "visual".
|
|
|
|
|
I guessed Visual C because this message board contains mostly windows development related posts. If OP needs another platform then modifying the search parameters isn't difficult. RS232 is a topic for which you can find very good tutorials on any platform very easily just by googling. I think OP was simply lazy to try that before posting here.
|
|
|
|